Difference between wait-notify and CountDownLatch
concurrency, java, synchronization
Solution
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.
Problem
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 ?