Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

what's the difference between CyclicBarrier/CountDownLatch and join in Java? What's the advantage of CyclicBarrier and CountDownLatch? In my opinion, just use join we can wait for a thread complete its execution.

like image 464
coderz Avatar asked Feb 16 '14 08:02

coderz


People also ask

What is difference between CyclicBarrier and CountDownLatch in Java?

As stated in the definitions, CyclicBarrier allows a number of threads to wait on each other, whereas CountDownLatch allows one or more threads to wait for a number of tasks to complete. In short, CyclicBarrier maintains a count of threads whereas CountDownLatch maintains a count of tasks.

Can CyclicBarrier and CountDownLatch in Java be used?

CountDownLatch and CyclicBarrier both used in multithreading environment and they both are part of. CountDownLatch − A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

What is the use of CountDownLatch in Java?

A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown() .

What is CyclicBarrier Java?

A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other.


2 Answers

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.

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.

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.

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.

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.

You can find some good examples at:

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

like image 84
AnatolyG Avatar answered Sep 18 '22 19:09

AnatolyG


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).

The javadoc gives a concrete example of the use of CountDownLatch. Read it.

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

like image 38
JB Nizet Avatar answered Sep 19 '22 19:09

JB Nizet