Similarity and Difference between CyclicBarrier and CountDownLatch in Java


You must read : CountDownLatch in java

CyclicBarrier in java



Similarity and Important Differences between CyclicBarrier and CountDownLatch in Java >


  1. First let’s discuss Similarity between CyclicBarrier and CountDownLatch in Java. CyclicBarrier and CountDownLatch are similar because they wait for specified number of thread to reach certain point and make count/parties equal to 0. But ,
for completing wait in CountDownLatch specified number of threads must call countDown() method in Java.
for completing wait in CyclicBarrier specified number of threads must call await() method in Java.

  1. Let’ see there constructor’s in Java >

CountDownLatch(int count)  
CountDownLatch is initialized with given count.
count specifies the number of events that must occur before latch is released.
CyclicBarrier(int parties)
New CyclicBarrier is created where parties number of thread wait for each other to reach common barrier point, when all threads have reached common barrier point, parties number of waiting threads are released.


  1. This is very important difference between CyclicBarrier and CountDownLatch in java. CyclicBarrier can be awaited repeatedly, but CountDownLatch can’t be awaited repeatedly. i.e. once count has become 0 cyclicBarrier can be used again but CountDownLatch cannot be used again in Java.


  1. Another important difference between CyclicBarrier and CountDownLatch in java. CyclicBarrier can be used to trigger event, but CountDownLatch can’t be used to launch event. i.e. once count has become 0 cyclicBarrier can trigger event in Java but CountDownLatch can’t.

How can cyclicBarrier launch event in java?
CyclicBarrier provides constructor for triggering event in Java.
CyclicBarrier(int parties, Runnable barrierAction)
New CyclicBarrier is created where parties number of thread wait for each other to reach common barrier point, when all threads have reached common barrier point, parties number of waiting threads are released and barrierAction (event) is triggered in Java.


So, in this thread concurrency tutorial we read what are Similarity and important Difference between CyclicBarrier and CountDownLatch in Java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

RELATED LINKS>

Executor and ExecutorService framework in java

Semaphore in java

ReadWriteLock and ReentrantReadWriteLock in java

CountDownLatch in java

CyclicBarrier in java



Important Similarity and Differences in Java >

Differences between execute() and submit() method of executor framework

Difference between synchronized and ReentrantLock in Java


eEdit
Must read for you :