譬如下面的两个条件变量 isEmpty和isFull
当调用他们的await()时,lock.lock()上的锁会被释放吗?
这里有3个对象,一个i哦是lock,一个是isEmpty,一个是isFull
上锁的对象是lock,而不是isEmpty或者isFull
那isFull或者isEmpty释放的锁确实lock上的?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
迷茫2017-04-18 10:50:30
await’s doc has this sentence: The lock associated with this Condition is atomically released
PHP中文网2017-04-18 10:50:30
Thanks for the invitation,
Answer:
1 2 3 |
|
The function ofawait() is to allow other threads to access competing resources, so the suspended state is to release the lock of the competing resources. In the java.util.concurrent class library of java
SE5, the basic class that uses mutual exclusion and allows task suspension is Condition. You can suspend a task through await(). When the external condition changes, it means that a certain The task can continue to execute, and you can notify the task through signal().Each lock() call must be followed by a try-finally clause to ensure that the lock can be released under all circumstances. The task must own this lock before it can call await(), signal(), signalAll().