Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between wait-notify and CountDownLatch

I need some help understanding the advantages of using CountDownLatch over traditional wait-notify. I think notifyAll() indeed does the same thing, and it seems easier to use (maybe because of familiarity).

Also, what's the difference between wait() and await() from CountDownLatch ?

Thanks !

EDIT : I guess I need to rephrase my queries :

Await() as per the docs says :

Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.

For me it's hard to see the difference between wait() and await() - await() is indeed using wait() under covers, and seems there is an implicit notifyAll() when count reached zero.

What I meant to ask was, why shouldn't I simply use a wait-notifyAll() mechanism (with my own counter variable processing), rather than going for CountDownLatch ?

like image 724
dev Avatar asked May 15 '12 04:05

dev


People also ask

What is a CountDownLatch?

CountDownLatch class is a synchronization aid which allows one or more thread to wait until the mandatory operations are performed by other threads. CountDownLatch is initialized with a given count of threads which are required to be completed before the main thread.

What is the use of CountDownLatch in Java?

Class CountDownLatch. A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. A CountDownLatch is initialized with a given count.

What is Java Util Concurrent CountDownLatch?

A java. util. concurrent. CountDownLatch is a concurrency construct that allows one or more threads to wait for a given set of operations to complete. A CountDownLatch is initialized with a given count.


1 Answers

They certainly don't do the same thing: CountDownLatch only signals when the event count has reached 0 and it does so automatically, wait-notify requires you to keep your own count if you want to achieve the same behavior. Implementing the same behavior is often error prone and it's best that you avoid it (especially if you're new to concurrency programming). Comparing CountDownLatch and wait-notify is hardly even an apples to oranges comparison, it's more like comparing an automatic drill and an Allen wrench.

I don't know if you've used notifyAll() and CountDownLatch, but notifyAll() alone will not give you the same behavior unless you've kept count of how many events have occurred. CountDownLatch is probably most suitable for performing a fixed number of tasks and waiting for those tasks to complete before you resume execution of the rest of your program. It's especially helpful when you have a fixed number of threads (e.g. ThreadPool) executing a fixed number of tasks, but your threads are way fewer than the tasks and you have to reuse them. With a CountDownLatch you can easily wait for all of the tasks to be completed. I don't know how you've been using notifyAll() to achieve the same behavior, but if you provide us with more information we can address which one of the two is a better choice (there are certainly some cases where waitNotify() is more appropriate).

Regarding the difference between wait() and await(), I'm somewhat disappointed in you! Looking up the documentation is step one of any question:

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html

await() is an actual function of CountDownLatch whereas wait() is inherited from Object. I would recommend that you check the documentation for what they do.

like image 58
Kiril Avatar answered Sep 19 '22 23:09

Kiril