[英]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.wait
, object.notify
和synchronized blocks
来解决生产者消费者类型的问题。 At the same time I can use locks
and conditions
from java.util.concurrent
package. 同时我可以使用
java.util.concurrent
包中的locks
和conditions
。 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.wait
和notify
来使线程在某些条件下等待,如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.