This is not a question of an official exam. It is a question extracted from the certification book named "SCJP Sun Certified Programmer for Java 5 Study Guide", and it appears on the page 750.
I thought that when a thread calls wait() over an object, and then someother thread calls notify(), the first one leaved the "waiting" state, and goes to the "locked" state, waiting for the second one to leave the synchronized region, and finally goes to "runnable".
But just when i thought that it was fairly clear, I found a question like this one:
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 object B is notified. D. Two seconds after lock B is released.
Which one is the correct?
The books says that A, but I simply don't get it, to me, there are no correct question.
public final void wait(long timeout)throws InterruptedException when wait() is called, the thread becomes disabled for scheduling and lies dormant until one of four things occur:
1. another thread invokes the notify() method for this object and the scheduler arbitrarily chooses to run the thread 2. another thread invokes the notifyAll() method for this object 3. another thread interrupts this thread 4. the specified wait() time elapses //see this one
So,After calling wait() method, thread A will become a candidate to get another turn at the CPU,after object B is notified, or after two seconds(wait(2000)) in your example.
Thanks&Regards<br />Yogi MaheshNath
Rafael De la Guetto
Joined: Jan 10, 2008
Thanks for your answer.
I agree with you, but given that the methods notify() and wait() must be called from a SYNCHRONIZED section, it is "impossible" for a thread to travel from "waiting" to "runnable", because it must visit the "locked" state waiting for the thread that calls the notify() method to abandom the synchronized region in wich it neccessarily is.
B. After the lock on B is released, or after two seconds.
Wrong, but debateable. You can release the lock, and not send the notification, and the thread will not wake up. However, you can argue that two seconds later it will wake up, which technically is still *after* the lock is released.
C. Two seconds after object B is notified.
Clearly wrong. If you sleep for 2 seconds, then send the notification, the thread will wakeup immediately (assuming that it can get the lock).
D. Two seconds after lock B is released.
Clearly wrong. If you sleep for 2 seconds, then release the lock, the thread will wakeup immediately -- and not two seconds after. (assuming that it can get the lock).
A. After object B is notified, or after two seconds.
Mostly right. It will wakeup sometime *after* getting the notification. Or it will wakeup sometime *after* 2 seconds. The time to acquire the lock doesn't change that -- it will still be after. Of course, you can argue that the *after* implies *immediately after*...
Which one is the correct?
Now, you have 2 answers that are clearly wrong. One that is wrong, but you can argue that it is right, because one part covers the other. And one that is right, but you can argue that it is wrong, because you imply that the word after assumes immediately.