In talking about the difference between Java Concurrency CountDownLatch CyclicBarrier

CountDownLatch CyclicBarrier and concurrency control are born, in the java.util.concurrent package.

CountDownLatch

After simply called counter for a multiple threads or wait for another thread is finished and he started to perform. The most important way is to await and countDown, calling await () If CountDownLatch internal thread count variable is not 0, then blocks until zero, and there may be more than one thread calls await (), that is, multiple threads simultaneously wait.

The following example first creates two threads, has been waiting for, then start 10 threads on CountDownLatch be reduced. After 10 countDown (), the first two threads to run.

public class Main2 {
    static CountDownLatch countDownLatch = new CountDownLatch(10);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread()+" start");
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + " end");

            }).start();
        }
        Thread.sleep(2000);
        startThreads();
    }

    private static void startThreads() {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                countDownLatch.countDown();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

CyclicBarrier

And CountDownLatch similar, but also to achieve a thread count between waiting for the same call its await () thread to blocking, but the difference is only when the other thread also calls await () until the number when the total number of counts to get run. And provides a method for resetting a reset ().

For example, a friend and opened the black, five friends turn to the Internet to people who did not come to wait, after attendance, together say long live sound alliance.

public class Main2 {
    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, () -> {
            System.out.println("可以开黑了");
        });
        for (int i = 0; i < 5; i++) {
            new Friend(cyclicBarrier).start();
            System.out.println("还剩" + (cyclicBarrier.getParties() - cyclicBarrier.getNumberWaiting()) + " 人");
            Thread.sleep(1000);

        }
    }

    static class Friend extends Thread {
        CyclicBarrier mCyclicBarrier;

        public Friend(CyclicBarrier cyclicBarrier) {
            mCyclicBarrier = cyclicBarrier;
        }

        @Override
        public void run() {
            super.run();
            String msg = (mCyclicBarrier.getParties() - mCyclicBarrier.getNumberWaiting() == 1) ? "最后一位到了" : "等待其他队友";
            System.out.println(Thread.currentThread() + msg);
            try {
                mCyclicBarrier.await();
                System.out.println("联盟万岁");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
}
还剩5 人
Thread[Thread-0,5,main]等待其他队友
还剩4 人
Thread[Thread-1,5,main]等待其他队友
还剩3 人
Thread[Thread-2,5,main]等待其他队友
还剩2 人
Thread[Thread-3,5,main]等待其他队友
还剩1 人
Thread[Thread-4,5,main]最后一位到了
可以开黑了
联盟万岁
联盟万岁
联盟万岁
联盟万岁
联盟万岁

the difference

For CountDownLatch, after waiting for the completion of the implementation of those conditions is another thread, but CyclicBarrier is itself, and CyclicBarrier can be recycled, provided reset () reset, but will lead to await () thread BrokenBarrierException is thrown.

That is CountDownLatch one or more threads wait for the other thread, but CyclicBarrier multiple threads wait for each other.

Published 42 original articles · won praise 7 · views 7752

Guess you like

Origin blog.csdn.net/HouXinLin_CSDN/article/details/104193588