简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM