Originally posted by Ajith Kallambella:
Why possibly longer? The thread will sleep for at most 500 ms. If it is interrupted before the time expires, it will come out of the sleeping state, otherwise it will sleep for the specified time ie., 500 ms. After coming out, it becomes ready to run again.
The thread scheduler won't schedule the thread for execution until the 500 ms have elapsed. Then the thread will at some point be made eligible for scheduling; however, there's no way to predict how long that will take. And once a thread is eligible to be run, there is no way to predict when it will actually be executed. So after the thread sleeps for 500ms, there will be some additional "scheduling overhead time" that we cannot predict exactly; thus the statement that it will sleep at least 500ms, but in most cases, a little bit more, and sometimes, theoretically, a lot more. It all depends on the scheduler.
Although you are right, IMO answer D is ambiguously worded.
the "sleeping "Threads always have the lock on the Object
If "the" sleeping thread means the thread in question, then the answer is right. However, if "the sleeping threads" mean any thread that is sleeping, then the answer is wrong since we don't know whether the call to sleep() is in a synchronized block.
I agree that the question is a little vague, but in context the answer makes sense. Perhaps they should have written "When Thread.sleep() is called on a thread which holds a lock, that thread maintains its locks while the thread sleeps."
[ February 26, 2002: Message edited by: Rob Ross ]