JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label CyclicBarrier. Show all posts
Showing posts with label CyclicBarrier. Show all posts

Difference between CountDownLatch and CyclicBarrier in java

8:32 AM
Lets see differences between CountDownLatch and CyclicBarrier. CountDownLatch vs CyclicBarrier Parameter CountDownLatch CyclicBarrier Reuse It can not be reused once count reaches 0 It can be reinitialized once parties reaches to 0, so it can reused Method  It calls countDown() method to reduce the counter It calls await() method to reduce the counter. Common Event It can not trigger common event when count reaches 0 It can trigger common event (Runnable) once reaches to a barrier point.  Constructor :CyclicBarrier(int parties, Runnable barrierAction) Constructor CountDownLatch(int count) CyclicBarrier(int parties)

Java CyclicBarrier Example

8:31 AM
CyclicBarrier is synchronized aid which allows set of threads to wait for each other at common barrier points.It is called cyclic because it can be reused once waiting threads are released. For example: Let’s say you have 3 threads, you want all threads(terms as parties) to reach a common point and then only they should proceed ahead.In this case, you can use CyclicBarrier with 3 parties and once 3 threads reach a common point, you can call an event which will implement runnable interface and Three threads will be released. Difference between CountDownLatch and CyclicBarrier The major difference between CyclicBarrier and CoundDownLatch is that CyclicBarrier can be reused.You can not use CountDownLatch once used. You can read more differences between CountDownLatch and CyclicBarrier. Java CyclicBarrier example: Step 1:  Create a file named “RunnableTask.java” in package .src.org.arpit.java2blog 1 2 3 4 5 6 7 8 9 10 11 12 13 1

.