CyclicBarrier/CountDownLatch 和 Java 中的 join 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21808814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:47:10  来源:igfitidea点击:

what's the difference between CyclicBarrier/CountDownLatch and join in Java?

javaconcurrencyjava.util.concurrent

提问by coderz

what's the difference between CyclicBarrier/CountDownLatchand joinin Java? What's the advantage of CyclicBarrierand CountDownLatch? In my opinion, just use joinwe can wait for a thread complete its execution.

Java 中的CyclicBarrier/CountDownLatch和有什么区别join?有什么优势CyclicBarrierCountDownLatch?在我看来,只要使用join我们就可以等待一个线程完成它的执行。

采纳答案by AnatolyG

Yes, "t.join()" makes the current thread waiting for "t" thread is finished and we can prepare a chain of threads when a thread is waiting for some other. But sometimes CountDownLatch/CyclicBarrier are more convenient.

是的,“t.join()”使当前线程等待“t”线程完成,当一个线程正在等待其他线程时,我们可以准备一个线程链。但有时 CountDownLatch/CyclicBarrier 更方便。

First of all, CountDownLatch/CyclicBarrier don't require all working threads should be finished. The threads can be running all the time the application is running. They just let us say that "some work" is done a number of times. Moreover, if we have N jobs and M threads and N > M, some threads can do a job several times until their common barier N is 0. This example shows that CountDownLatch/CyclicBarrier are very useful primitives to share N tasks between M threads.

首先,CountDownLatch/CyclicBarrier 不需要所有工作线程都应该完成。线程可以在应用程序运行的所有时间运行。他们只是让我们说“一些工作”做了很多次。此外,如果我们有 N 个作业和 M 个线程,并且 N > M,则某些线程可以执行多次作业,直到它们的公共屏障 N 为 0。此示例表明 CountDownLatch/CyclicBarrier 是在 M 个线程之间共享 N 个任务的非常有用的原语。

Also, to use join(), each thread should have a reference to another thread to call join(). It makes your code a bit dirty especially when you have more than 2 working threads. Sharing of one instance of CountDownLatch/CyclicBarrier looks more clear.

此外,要使用 join(),每个线程都应该引用另一个线程来调用 join()。它使您的代码有点脏,尤其是当您有 2 个以上的工作线程时。CountDownLatch/CyclicBarrier 的一个实例的共享看起来更清晰。

The main difference between CyclicBarrier and CountDownLatch is that CyclicBarrier is reusable and CountDownLatch is not. You can reuse CyclicBarrier by calling reset() method which resets the barrier to its initial state.

CyclicBarrier 和 CountDownLatch 之间的主要区别在于 CyclicBarrier 是可重用的,而 CountDownLatch 则不是。您可以通过调用 reset() 方法来重用 CyclicBarrier,该方法将屏障重置为其初始状态。

CountDownLatch is good for one time event like application/module start-up time and CyclicBarrier can be used to in case of recurrent event e.g. concurrently (re-)calculating each time when the input data changed.

CountDownLatch 适用于一次性事件,例如应用程序/模块启动时间,而 CyclicBarrier 可用于重复事件,例如每次输入数据更改时并发(重新)计算。

You can find some good examples at:

您可以在以下位置找到一些很好的示例:

http://javarevisited.blogspot.sg/2012/07/countdownlatch-example-in-java.htmlhttp://javarevisited.blogspot.ru/2012/07/cyclicbarrier-example-java-5-concurrency-tutorial.html

http://javarevisited.blogspot.sg/2012/07/countdownlatch-example-in-java.html http://javarevisited.blogspot.ru/2012/07/cyclicbarrier-example-java-5-concurrency-tutorial.html

回答by JB Nizet

join()waits for one thread to complete. CountDownLatch.await()allows N threads to wait until the countdown reaches 0. It can be used to make sure N threads start doing something at the same time (starting a race, for example), or to wake up another thread once N threads have reached a given point (the end of a race, for example).

join()等待一个线程完成。CountDownLatch.await()允许 N 个线程等待,直到倒计时达到 0。它可用于确保 N 个线程同时开始做某事(例如,开始竞争),或者在 N 个线程达到给定点后唤醒另一个线程(例如,比赛结束)。

The javadocgives a concrete example of the use of CountDownLatch. Read it.

javadoc给出了使用 CountDownLatch 的具体示例。阅读。

CyclicBarrier is similar to CountDownLatch, but allows for regular coordination points.

CyclicBarrier 类似于 CountDownLatch,但允许定期协调点。