简体   繁体   English

Java并发锁和条件使用

[英]Java concurrency lock and condition usage

I can use object.wait , object.notify and synchronized blocks to solve the producer consumer type of problems. 我可以使用object.waitobject.notifysynchronized blocks来解决生产者消费者类型的问题。 At the same time I can use locks and conditions from java.util.concurrent package. 同时我可以使用java.util.concurrent包中的locksconditions 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. 我确信我无法理解为什么我们需要条件才能使用object.waitnotify来使线程在某些条件下等待,如queue为空或满。 Is there any other benefit we are getting if we use java.util.concurrent.locks.Condition ? 如果我们使用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. 正如Lock是同步的一般化一样,Lock框架包括一个名为Condition的wait和notify的泛化。 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. Lock对象充当绑定到该锁的条件变量的工厂对象,与标准的wait和notify方法不同,可以有多个与给定Lock关联的条件变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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