本文参考:
wait(),notify() 与 await(), signal(), signalAll() 的区别
wait()
,notify()
与await()
,signal()
,signalAll()
的一个明显区别就是前者属于Object
,而后者属于Condition
。官方文档对await()
和signal()
有如下说明:
Condition factors out the Object monitor methods (wait, notify and
notifyAll) into distinct objects to give the effect of having multiple
wait-sets per object, by combining them with the use of arbitrary Lock
implementations.
翻译过来就是Condition
接口把Object
中的wait()
、notify()
和notifyAll()
分解到了不同对象中,搭配上任意一种Lock使用,使得一个对象可以存在于多个等待集。
等待集(waitset)
- 每个对象都有一个等待集,该等待集是一个线程的集合。
wait()
、notify()
和notifyAll()
方法的调用会对等待集中的线程进行移入或移除操作。
所以, await() 和 signall 的添加, 实际上是为我们提供了一种方便的基于同一个锁, 实现多个条件的 wait() 和 notify() 操作。更多例子请参考原文wait(),notify() 与 await(), signal(), signalAll() 的区别。