?
Which of the following stops execution of a thread?
Answer & Solution
Correct Answer:
Option
B
Understanding Thread Execution Control in Java
Let's break down why one of these options stops a thread's execution.
Option A: Calling SetPriority() method on a Thread object
It doesn't stop the thread; it just suggests a priority.
Option B: Calling notify() method on an object
It wakes up a single thread that's waiting on the object's monitor.
It doesn't stop the currently running thread; it potentially allows another thread to run.
Option C: Calling wait() method on an object
The thread releases the lock on the object and waits until another thread calls
This is the correct answer.
Option D: Calling read() method on an InputStream object
If no data is available, the thread might block temporarily until data becomes available.
While it *can* appear to pause the thread, it's waiting for I/O, not explicitly stopping execution in the same way as
The thread remains alive.
In Summary: The
Let's break down why one of these options stops a thread's execution.
Option A: Calling SetPriority() method on a Thread object
setPriority() is used to influence the scheduler to give a thread more or less CPU time.It doesn't stop the thread; it just suggests a priority.
Option B: Calling notify() method on an object
notify() is used for thread communication.It wakes up a single thread that's waiting on the object's monitor.
It doesn't stop the currently running thread; it potentially allows another thread to run.
Option C: Calling wait() method on an object
wait() causes the current thread to pause its execution and enter a waiting state.The thread releases the lock on the object and waits until another thread calls
notify() or notifyAll() on the same object.This is the correct answer.
Option D: Calling read() method on an InputStream object
read() is used to read data from an input stream.If no data is available, the thread might block temporarily until data becomes available.
While it *can* appear to pause the thread, it's waiting for I/O, not explicitly stopping execution in the same way as
wait().The thread remains alive.
In Summary: The
wait() method directly suspends a thread's execution and places it in a waiting state, making Option C the correct answer.
Join the Discussion
Login to post a comment or share your explanation.
LoginHow wait() works:
When a thread calls wait() on an object, it releases the object's monitor and enters a waiting state
The thread remains stopped until either:
Another thread calls notify() or notifyAll() on the same object
The thread is interrupted
A specified timeout period elapses (if using wait(long timeout))