Discussion :: Threads
-
Which two statements are true?
- Deadlock will not occur if wait()/notify() is used
- A thread will resume execution as soon as its sleep duration expires.
- Synchronization can prevent two objects from being accessed by the same thread.
- The wait() method is overloaded to accept a duration.
- The notify() method is overloaded to accept a duration.
- Both wait() and notify() must be called from a synchronized context.
Answer : Option C
Explanation :
Statements (4) and (6) are correct. (4) is correct because the wait() method is overloaded to accept a wait duration in milliseconds. If the thread has not been notified by the time the wait duration has elapsed, then the thread will move back to runnable even without having been notified.
(6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, context. A thread must own the lock on the object its invoking wait()/notify()/notifyAll() on.
(1) is incorrect because wait()/notify() will not prevent deadlock.
(2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might not necessarily resume execution right away. To resume executing, the newly awakened thread must still be moved from runnable to running by the scheduler.
(3) is incorrect because synchronization prevents two or more threads from accessing the same object.
(5) is incorrect because notify() is not overloaded to accept a duration.
Be The First To Comment