【线程八】【翻译●中英双语】Java中CyclicBarrier 和 CountDownLatch有什么不同?

引言

在 理论物理学 中, M理论 是 弦理论 的一种延展理
论。M理论当中指出,描述完整的物理世界一共需
要十一个 维度 ,其维度超过弦理论所需要的十维,
被支持者相信该理论统合了所有五种弦理论,并成
为终极的物理理论

CyclicBarrier CountDownLatch 都可以用来让一组线程等待其它线程。与 CyclicBarrier 不同的是,CountdownLatch 不能重新使用

 What is CyclicBarrier in Java

CyclicBarrier in Java is a synchronizer introduced in JDK 5 on java.util.Concurrent package along with other concurrent utility like Counting SemaphoreBlockingQueueConcurrentHashMap etc. CyclicBarrier is similar to CountDownLatch which we have seen in last article  What is CountDownLatch in Java and allows multiple threads to wait for each other (barrier) before proceeding. Difference between CountDownLatch and CyclicBarrier is a also very popular multi-threading interview question in Java. CyclicBarrier is a natural requirement for concurrent并发 program because it can be used to perform final part of task once individual tasks  are completed. All threads which wait for each other to reach barrier are called parties, CyclicBarrier is initialized with number of parties to be wait and threads wait for each other by calling CyclicBarrier.await() method which is a blocking method in Java and  blocks until all Thread or parties call await(). In general calling await() is shout out that Thread is waiting on barrier. await() is a blocking call but can be timed out or Interrupted by other thread. In this Java concurrency tutorial we will see What is CyclicBarrier in Java  and  an example of CyclicBarrier on which three Threads will wait for each other before proceeding further.

Java 中的 CyclicBarrier 是一个同步器,同时 JDK 5 java.util.Concurrent 其他并行的实用程序也被介绍了,如 Counting Semaphore计数信号量、 BlockingQueue ConcurrentHashMap 等。CyclicBarrier 是类似于 CountDownLatch CountDownLatch 介绍),允许多个线程在处理之前等待对方。CountDownLatch CyclicBarrier 之间的区别是 Java 中很受欢迎的多线程面试问题。CyclicBarrier 是并发程序自然要求,因为它可以用来执行任务完成后的最后一部分。等待对方达到屏障的所有线程都称为缔约方,CyclicBarrier 用一些缔约方将等待初始化和线程等待对方通过调用 CyclicBarrier.await() 方法,直到所有的线程或缔约方调用 await() 是一个 Java 和块中的阻止方法。在调用 await() 一般喊出该线程正在等待的屏障。await() 是一个阻塞调用,但可以超时或被其他线程中断(这段其实意思是此方法可以使大家像跑步一样,在一个起点等着,然后一块跑!)。本 Java 并发教程中我们将看到什么是在 Java CyclicBarrier 的三个线程将等待对方在进一步行动之前的示例中的 CyclicBarrier

Difference between CountDownLatch and CyclicBarrier in Java

In our last article we have see how CountDownLatch can be used to implement multiple threads waiting for each other. If you look at CyclicBarrier it also the does the same thing but there is a different you can not reuse CountDownLatch once count reaches zero while you can reuse CyclicBarrier by calling reset() method which resets Barrier to its initial State. What it implies that CountDownLatch is good for one time event like application start-up time and CyclicBarrier can be used to in case of recurrent event e.g. concurrently calculating solution of big problem etc. If you like to learn more about threading and concurrency in Java you can also check my post on When to use Volatile variable in Java  and How Synchronization works in Java.

两者区别

在上篇文章中,介绍了CountDownLatch 可以实现多线程中等待彼此的情况。CyclicBarrier 也能干这事,不同之处,CountDownLatch 不能再次从0开始,但是CyclicBarrier 通过调用reset可以把等待的起点初始化。

这意味着CountDownLatch 适用于一次性的,如app启动时间。CyclicBarrier 可以被用于并发事件。如并发计算解决大问题。如果想了解更多线程和并发,看原文链接。

CyclicBarrier in Java – Example

CyclicBarrier 示例

Java CyclicBarrier Example and Tutorial program

Now we know what is CyclicBarrier in Java and it’s time to see example of CyclicBarrier in Java. Here is a simple example of CyclicBarrier in Java on which we initialize CyclicBarrier with 3 parties, means in order to cross barrier, 3 thread needs to call await() method. each thread calls await method in short duration but they don’t proceed until all 3 threads reached barrier, once all threadreach barrier, barrier gets broker and each thread started there execution from that point. Its much clear with the output of following example of CyclicBarrier in Java:

我们现在new3CyclicBarrier ,意思是为了到达起点(barrier),3个线程需要调await()方法,每个线程调用await方法后,并不往下走,直到所有线程都到起点后,一旦都到了,所有线程就从这个点开撸了,运行结果很清楚。

输出

Fork From Git

When to use CyclicBarrier in Java

神马时候用CyclicBarrier

Given the nature of CyclicBarrier it can be very handy to implement map reduce kind of task similar to fork-join framework of Java 7, where a big task is broker down into smaller pieces and to complete the task you need output from individual small task e.g. to count population of India you can have 4 threads which counts population from North, South, East and West and once complete they can wait for each other, When last thread completed there task, Main thread or any other thread can add result from each zone and print total population. You can use CyclicBarrier in Java :

1) To implement multi player game which can not begin until all player has joined.

2) Perform lengthy calculation by breaking it into smaller individual tasks, In general to implement Map reduce technique.

鉴于它可以很方便的实施的 CyclicBarrier map reduce的任务类似于 Java 7 的 fork-join framework,map reduce是一个大的任务分成小块,然后完成这项大任务,你需要完成每个单个小任务。如查印度人口,你可以有 4 个线程从北部、 南部、 东部和西部,每个完成后,他们可以等待对方,当最后一个线程完成,主线程或任何其他线程可以从每个区域中添加结果并打印人口总数。您可以使用 Java 的 CyclicBarrier像这样:

 

Important point of CyclicBarrier in Java

  1. CyclicBarriercan perform a completion task once all thread reaches to barrier, This can be provided while creating CyclicBarrier.
  2. IfCyclicBarrieris initialized with 3 parties means 3 thread needs to call await method to break the barrier.
  3. Thread will blockon await() until all parties reaches to barrier, another thread interrupt or await timed out.
  4. If another thread interrupt the thread which is waiting on barrier it will throwBrokernBarrierExceptionas shown below:

 

5.CyclicBarrier.reset() put Barrier on its initial state, other thread which is waiting or not yet reached barrier will terminate with java.util.concurrent.BrokenBarrierException.

That’s all on  What is CyclicBarrier in Java , When to use CyclicBarrier in Java and a Simple Example of How to use CyclicBarrier in Java . We have also seen difference between CountDownLatch and CyclicBarrier in Java and got some idea where we can use CyclicBarrier in Java Concurrent code.

  1. CyclicBarrier 可以执行完成一种一旦所有线程都到达的某点时,这个总任务就完成的工作。
  1. 如果CyclicBarrier new了3个,那么意味着这3个都要调用await在起点上等待
  2. 其它线程被打断或者等待超时。
  3. 如果其它线程打断了在起点上等待的线程,它将会抛BrokernBarrierException ,像下面这样
java.util.concurrent.BrokenBarrierException

at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:172)

at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:327)

 

5.CyclicBarrier.reset()方法可使起点到初始状态,其它还在等待状态或者还木有到达barrier 的线程将会终止并抛BrokenBarrierException异常。

Yol总结:

1 CyclicBarrier可以在多线程使用,并可以设置一个点,大家都停在这里,通过CyclicBarrier.wait(),到这点后,每个线程就又开始各干各的,就像交叉线一样。不同的是,线程间的数据,可以在这时候做集合,也可以做分散(数组)。

2 多线程通过testCyclicBarrier启动,同时启动的还有CyclicBarrier,CyclicBarrier有两个构造方法,里面传的参数是指定要几个线程到达障碍点才会停,还有其实我觉得第一个就可以用了,第二种(下面这种)就是多了个提示而已。

 

3 新启动的多线程都会到ComponentThread里面,并且ComponentThread里面已经传过来了CyclicBarrier,这时候线程都已经不能程度的到了run方法里面。直到barrier.await();,大家都停了下来,直到指定数量(构造方法)的线程到完之后 ,才会再往下走。最后大家还会再交叉分开。各自走各自的。

发表评论

电子邮件地址不会被公开。