About Question com.enthuware.ets.scjp.v6.2.671 :
Posted: Wed Jun 17, 2015 7:51 pm
Which of the following may pause/stop the current thread?
1. calling Thread.yield()
2. calling someObject.wait()
3. calling someObj.notify()
4. calling end() method on the Thread object.
Correct answer: 1,2
Please explain why '3' is not correct. Reason being: For a thread (currentThread) that's holding a lock on someObj, calling notify on currentThread may unblock other threads that were waiting for the lock on someObj and cause the current thread to move from running to runnable. Thread.yield() is correct but again its not guaranteed to put current thread to sleep.
1. calling Thread.yield()
2. calling someObject.wait()
3. calling someObj.notify()
4. calling end() method on the Thread object.
Correct answer: 1,2
Please explain why '3' is not correct. Reason being: For a thread (currentThread) that's holding a lock on someObj, calling notify on currentThread may unblock other threads that were waiting for the lock on someObj and cause the current thread to move from running to runnable. Thread.yield() is correct but again its not guaranteed to put current thread to sleep.