譬如下面的两个条件变量 isEmpty和isFull
当调用他们的await()时,lock.lock()上的锁会被释放吗?
这里有3个对象,一个i哦是lock,一个是isEmpty,一个是isFull
上锁的对象是lock,而不是isEmpty或者isFull
那isFull或者isEmpty释放的锁确实lock上的?
private volatile boolean usedData = true;//mutex for data
private final Lock lock = new ReentrantLock();
private final Condition isEmpty = lock.newCondition();
private final Condition isFull = lock.newCondition();
public void setData(int data) throws InterruptedException {
lock.lock();
try {
while(!usedData) {//wait for data to be used
isEmpty.await();
}
this.data = data;
isFull.signal();//broadcast that the data is now full.
usedData = false;//tell others I created new data.
}finally {
lock.unlock();//interrupt or not, release lock
}
}
await’s doc has this sentence: The lock associated with this Condition is atomically released
Thanks for the invitation,
Answer: