Found this on a mock exam......
public synchronized void someMethod()
{
//lots of code
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{ }
}
1. The code causes compilation error - sleep cannot be called inside synchronized methods.
2. The code causes compilation error - sleep is not a static method of java.lang.Thread
3. The
Thread sleeps for at least 500 milliseconds in this method if not interrupted.
4. When the thread "goes to sleep" it releases the lock on the object.
5. The "sleeping" Threads always have the lock on the Object.
The answer given is 3 & 5. I didn't find this correct.... I think the answer is 3 & 4 as a Thread does give up the lock on an object if it goes to sleep.
Can somebody please verify this..???
Thanks,
Shafeeq