Shamik
Shamik

Reputation: 7108

Java concurrency lock and condition usage

I can use object.wait ,object.notify and synchronized blocks to solve the producer consumer type of problems. At the same time I can use locks and conditions from java.util.concurrent package. I am sure I am not able to understand why we need conditions when we can use object.wait and notify to make threads waiting on some condition like queue is empty or full. Is there any other benefit we are getting if we use java.util.concurrent.locks.Condition ?

Upvotes: 3

Views: 4677

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272257

This article provides a good explanation:

Just as Lock is a generalization for synchronization, the Lock framework includes a generalization of wait and notify called Condition. A Lock object acts as a factory object for condition variables bound to that lock, and unlike with the standard wait and notify methods, there can be more than one condition variable associated with a given Lock.

Upvotes: 4

Related Questions