Threads — which three operations guarantee that the <em>current</em> thread will leave the running state? Choose exactly three: yield(), wait(), notify(), notifyAll(), sleep(1000), aLiveThread.join(), Thread.killThread()

Difficulty: Medium

Correct Answer: 2, 5 and 6

Explanation:


Introduction / Context:
Understanding which APIs cause the calling thread to stop running is essential for reasoning about scheduling and liveness. Some calls affect other threads, while others suspend the current one.



Given Data / Assumptions:

  • We must identify calls that force the current thread to leave the running state.
  • Assume proper preconditions (e.g., monitor ownership when calling wait()).


Concept / Approach:
wait() suspends the current thread and releases the monitor; sleep(ms) suspends the current thread for at least the specified time; join() called on another live thread makes the current thread wait until that thread terminates. yield() only hints to the scheduler and does not guarantee leaving running. notify()/notifyAll() wake other waiting threads and do not block the caller. Thread.killThread() does not exist in Java.



Step-by-Step Solution:

wait() → current thread waits → guaranteed to stop running.sleep(1000) → current thread sleeps → guaranteed to stop running.aLiveThread.join() → current thread waits for target thread to die → guaranteed to stop running.yield() → advisory only, no guarantee.notify()/notifyAll() → affect others; caller continues.Thread.killThread() → non-existent.


Verification / Alternative check:
Javadoc confirms the blocking behavior for wait, sleep, and join.



Why Other Options Are Wrong:
They either do not block the current thread or are not real APIs.



Common Pitfalls:
Believing yield() guarantees descheduling; confusing the caller of notify() with the awakened thread.



Final Answer:
2, 5 and 6

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion