If I have the code:
ReentrantLock lock = new ReentrantLock();
Condition waiting = lock.newCondition();
Thread 1:
value = default;
lock.lock();
try {
waiting.await(new Long(timeout).longValue(), TimeUnit.SECONDS);
} catch (InterruptedException e) {
} finally {
lock.unlock();
}
Thread 2:
lock.lock();
//set value
waiting.signalAll();
lock.unlock();
Am I correct in that the monitor on the lock is released when await is called, allowing the event driven thread 2 to run if needed? If thread 2 happens to run, when will thread 1 be able to resume, upon signalAll(), or lock.unlock()? If thread 2 is signaling a wakeup, but still has a lock, how does that work??
The lock is in fact released when invoking
await. WhensignalAllis called, no waiting threads will awake until the signalling threadunlocksHowever, it is important to differentiate Java object monitors with Java Locks. They are separate constructs, in fact the ReentrantLock/Condition itself can be a monitor in a different context then what you are working with (for example if instead of
awaityou calledwaityou would get the obvious IllegalMonitorStateException).