Reputation: 2715
I have read this answer:
Difference between wait-notify and CountDownLatch
I know both process are different,
My question is more of the functional aspect:
Is there any situation which cannot be solved by wait/notify mechanism but can be solved only by CountDownLatch?
If no,then functionally, CountDownlatch was introduced solely to make coordination between threads easier and cleaner, right?
Upvotes: 2
Views: 2820
Reputation: 3568
Consider a case where you may not want to wait if a condition is met. You could get your hands dirty and probe a lock, but this is often buggy.
A CountDownLatch comes to the rescue, yes for convenience, but not solely to solve the wait/notify paradigm.
The obvious use of CountDownLatch as a way to wait for multiple conditions also comes to mind.
Why reinvent the wheel when it's available first party?
Upvotes: 1
Reputation: 81862
Sure you can create the same functionality just with wait, notify, synchronized and so on. CountDownLatch is a normal Java class implemented using such primitives. For details you can have a look at the actual source code: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/concurrent/CountDownLatch.java
Upvotes: 4
Reputation:
The classes in java.util.concurrent
are designed to make certain multithreading scenarios more easier to code and manage. You can use low-level constructs such as wait
and notify
but you really need to know what you are doing.
Here is the excerpt from the API:
Utility classes commonly useful in concurrent programming. This package includes a few small standardized extensible frameworks, as well as some classes that provide useful functionality and are otherwise tedious or difficult to implement.
Upvotes: 1