Im a little bit confused after reading
thread chapter on wait() and notify() methods.
There is a statement on page 752 in K&B
SCJP 6 book :
"(...), when notify() is caled, that doesn't mean the thread gives up its lock at the moment. If the thread is still
completing synchronized code, the lock is not released until the thread moves out of synchronized code. So just
because notify() is called doesn't mean the lock becomes available at that moment."
And there is this question 6 on page 775:
"Assume the following method is properly synchronized and called from a thread A on an object B:
wait(2000);
After calling this method, when will the thread A become a candidate to get another turn at the CPU?
A. After object B is notified, or after two seconds
B. After the lock on B is released, or after two seconds
C. Two seconds after B is notified
D. Two seconds after lock B is released
OK: A is correct. Either of two events will make the thread a candidate for running again.
WRONG: B is incorrect because a waiting thread will not return to runnable when the lock is released, unless a notification occurs.
C in incorrect because the thread will become a candidate
immediately after notification (...)"
My doubts are:
1. Given that notify() is not causing to release lock immediately how is A can be true?
2. Why B is wrong? notify() is not going to make A go until code reaches end of synchronize block that should enclose notify() call. So naturally A have his go after lock on B is released?
3. Book clarification on ansfer C is also wrong given previous statement about notify()?
thanks in advance,
Pawel