Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java concurrency lock and condition usage

Tags:

java

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 ?

like image 584
Shamik Avatar asked Feb 26 '26 15:02

Shamik


1 Answers

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.

like image 63
Brian Agnew Avatar answered Feb 28 '26 06:02

Brian Agnew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!