Which of the following cannot directly cause a Java thread to stop executing (i.e., leave the running state) at the moment of the call?

Difficulty: Easy

Correct Answer: Calling setPriority() on a Thread object

Explanation:


Introduction / Context:
Many APIs influence a thread’s execution state. Some block the current thread immediately; others merely request a change or affect different threads. Here we identify the call that does not directly deschedule the caller.



Given Data / Assumptions:

  • The question asks about direct, immediate effect on the current thread’s running state.


Concept / Approach:
setPriority() changes the scheduling priority but does not itself block or stop the caller. By contrast, wait() blocks the current thread, sleep() blocks the current thread, and I/O such as InputStream.read() often blocks until data is available. notify() affects waiting threads but does not block the caller; however, it also does not stop the caller.



Step-by-Step Reasoning:

setPriority() → adjusts scheduling metadata only → no immediate stop.wait() → caller releases monitor and blocks → immediate stop.sleep() → caller sleeps → immediate stop.read() → may block until data → likely stop.notify() → awakens others; caller continues executing.


Verification / Alternative check:
Javadoc behavior for the listed methods shows which ones are blocking versus non-blocking.



Why Other Options Are Wrong:
They represent blocking calls (wait/sleep/read) or do not answer the “cannot directly cause” aspect correctly.



Common Pitfalls:
Assuming priority changes force an immediate context switch; assuming notify() blocks the caller (it does not).



Final Answer:
Calling setPriority() on a Thread object

Discussion & Comments

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