One article they get to know four kinds of synchronization tools

CountDownLatch

Explanation:

CountDownLatch corresponds to a latch, the latch lock hung N. Only N locks are untied, then the door will open. How to understand it? Let me give you an example of a racing game, racing game must wait until all players are ready, the referee in order to open the starting gun. Players can start to run. CountDownLatch which there are two main methods, one is to await () will hang lock blocks the current thread, the starting point is equivalent to the referee stood waiting, waiting for you players are ready, a method is used to unlock countDown, the equivalent of players ready after countDown way to tell the referee to call myself ready, when everyone was ready to open after the referee starting gun.

Code:

public class TestCountDownLatch {
    public static void main(String[] args) {
        // 需要等待两个线程,所以传入参数为2
        CountDownLatch latch = new CountDownLatch(2);
        // 该线程运行1秒
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("1号选手准备就绪!用时1秒!");
                latch.countDown();
            }
        }).start();
        
        // 该线程运行3秒
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("2号选手准备就绪!用时3秒!");
                latch.countDown();
            }
        }).start();
        
        try {
            System.out.println("请1号选手和2号选手各就各位!");
            // 主线程在此等待两个线程执行完毕之后继续执行
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 两个线程执行完毕后,主线程恢复运行
        System.out.println("裁判发枪,1号选手和2号选手开跑!");
    }
}

复制代码

operation result:

请1号选手和2号选手各就各位!
1号选手准备就绪!用时1秒!
2号选手准备就绪!用时3秒!
裁判发枪,1号选手和2号选手开跑!
复制代码

If the effect of CountDownLatch get rid of it? Operating results will become as follows:

请1号选手和2号选手各就各位!
裁判发枪,1号选手和2号选手开跑!
1号选手准备就绪!用时1秒!
2号选手准备就绪!用时3秒!
复制代码

Referee will be open starting gun when the player is not ready, and this mess.
CountDownLatch actually one of the easiest use is to calculate the time when the finished multiple threads. Just as an example among the two threads execute in parallel it spent a total of 3 seconds.

CyclicBarrier

Explanation:

CyclicBarrier like a fence, the individual threads stopped. Cyclic cycle is in English, indicating that the tool can be recycled. CyclicBarrier (N) configuration parameters indicate that a total number of threads need to wait for each other. It is equivalent to the N players agreed to carry out many competitions, each finished the game must wait for each other at the starting point. The reader may be wondering right away and CountDownLatch this is not the same? Different. Because CountDownLatch players are waiting for the referee, be thread calls await () method, waits for the call countDown () method of each thread. The players are waiting CyclicBarrier players, calling await () method of the thread wait for each other after the other threads are waiting for a good run, run and then start the next round.

We give an example, two players for the game, a total of three rounds of competition.

Code:

public class TestCyclicBarrier {
    // 1号选手跑的轮数
    public static int countA = 1;
    // 2号选手跑的轮数
    public static int countB = 1;
    public static void main(String[] args) {
        // 填入2,代表2个线程互相等待
        CyclicBarrier barrier = new CyclicBarrier(2);

        new Thread(new Runnable() {
            @Override
            public void run() {
                // 一共跑三轮
                for (int i = 0; i < 3; i++) {
                    System.out.println("1号选手开始跑!当前第" + countA++ + "轮比赛!");
                    // 1号选手跑得慢,每次跑三秒
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        System.out.println("1号选手抵达终点!");
                        // 调用等待方法,在此等待其他选手
                        barrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                // 一共等待三轮
                for (int i = 0; i < 3; i++) {
                    System.out.println("2号选手开始跑!当前第" + countB++ + "轮比赛!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        System.out.println("2号选手抵达终点!");
                        // 调用等待方法,在此等待其他选手
                        barrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

}

复制代码

operation result:

1号选手开始跑!当前第1轮比赛!
2号选手开始跑!当前第1轮比赛!
2号选手抵达终点!
1号选手抵达终点!
1号选手开始跑!当前第2轮比赛!
2号选手开始跑!当前第2轮比赛!
2号选手抵达终点!
1号选手抵达终点!
1号选手开始跑!当前第3轮比赛!
2号选手开始跑!当前第3轮比赛!
2号选手抵达终点!
1号选手抵达终点!
复制代码

Each round No. 1 player and 2 players will return to the starting line waiting for each other, and then open the next round.
If not CyclicBarrier it?

1号选手开始跑!当前第1轮比赛!
2号选手开始跑!当前第1轮比赛!
2号选手抵达终点!
2号选手开始跑!当前第2轮比赛!
2号选手抵达终点!
2号选手开始跑!当前第3轮比赛!
1号选手抵达终点!
1号选手开始跑!当前第2轮比赛!
2号选手抵达终点!
1号选手抵达终点!
1号选手开始跑!当前第3轮比赛!
1号选手抵达终点!
复制代码

At this point No. 2 player directly to finish the three rounds of competition, ranging from the No. 1 player.

Semaphore

Semaphore English literally means semaphore. Its mechanism is that each thread you want to get a chance to run, it must obtain the semaphore. acquire () method of obtaining a blocking semaphore, release () release the semaphore. For example, suppose we go to Disney to play, but Disney worried that a lot of tourists, then, affect your play experience, so provisions can only sell two tickets per hour. So that it can control the number of visitors among the amusement park.

Code:

public class TestSemaphore {

    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(0);
        System.out.println("顾客在售票处等候中");
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; ; ) {
                    try {
                        Thread.sleep(500);
                        // 等待出票
                        semaphore.acquire();
                        System.out.println("顾客拿到门票入场!");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < 3; i++) {
                    try {
                        // 等待一小时再发门票
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    // 一次性发出两张门票
                    System.out.println("售票处第" + (i + 1) + "小时售出两张票!");
                    semaphore.release();
                    semaphore.release();
                }
            }
        }).start();

        System.out.println("售票处开始售票!");
    }
}
复制代码

operation result:

顾客在售票处等候中...
售票处开始售票!
售票处第1小时售出两张票!
顾客拿到门票入场!
顾客拿到门票入场!
售票处第2小时售出两张票!
顾客拿到门票入场!
顾客拿到门票入场!
售票处第3小时售出两张票!
顾客拿到门票入场!
顾客拿到门票入场!

复制代码

Exchanger

Explanation:

Exchanger allows two threads to provide a synchronization point to exchange data. Exchanger bit like two threads CyclicBarrier, between threads are waiting for each other, except that the Exchanger more operational exchange. For example, when playing online games like before, buyers and sellers must come to the same place on the map, like face to face transactions, paid their dues to pay and equipment.

Code:

public class TestExchanger {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                String weapon = "装备";
                System.out.println("我是卖家,我带着" + weapon + "过来了!");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("卖家到达地图上交易地点");
                try {
                    System.out.println("我是卖家,换回了" + exchanger.exchange(weapon));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String money = "一万游戏币";
                System.out.println("我是买家,我带着" + money + "过来了");
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("买家到达地图上交易地点");
                try {
                    System.out.println("我是买家,换回了" + exchanger.exchange(money));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

复制代码

operation result:

我是卖家,我带着装备过来了!
我是买家,我带着一万游戏币过来了
卖家达到交易地点
买家到达交易地点
我是买家,换回了装备
我是卖家,换回了一万游戏币
复制代码

If wrong, I implore User reviews correction.

Transfer from my personal blog vc2x.com

Guess you like

Origin juejin.im/post/5d9b5e5351882509751b12bc