Which Methods May Cause a Thread to Stop Executing?

By Priyanshu Vaish|Updated : July 1st, 2022

Which methods may cause a thread to stop executing?

  1. wait()
  2. notify()
  3. notifyall()
  4. exits synchronized code

Answer A. wait().

The wait() may cause a thread to stop executing.

Solution

Option A is correct.  The wait() method makes the active thread wait until another thread calls this object's notify() or notifyAll() methods.

Option B is incorrect. A single thread waiting on this object's monitor is awakened by the notify() function.

Option C is incorrect. Notifies every thread that is waiting on this object's monitor by calling notifyAll().

Option D is incorrect. The thread holding the lock or the thread in the synchronized method typically quits the synchronized method when a lock is released. The lock is released when another thread enters a synchronized procedure on that object. Does executing threads halt while entering or leaving synchronized code? Not required, as the thread can still execute unsynchronized code. The word directly in the question, in my opinion, gives us a hint. The execution of a thread is not immediately stopped when synchronized code is exited.

☛ Related Questions:

 

Comments

write a comment

Follow us for latest updates