Multiple choice

Which of the following will directly stop the execution of a thread?

  1. wait()

  2. notify()

  3. notifyall()

  4. exits synchronized code

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Option 1 is correct. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. Option 2 is wrong. notify() - wakes up a single thread that is waiting on this object's monitor. Option 3 is wrong. notifyAll() - wakes up all threads that are waiting on this object's monitor. Option 4 is wrong. Typically, releasing a lock means the thread holding the lock (in other words, the thread currently in the synchronized method) exits the synchronized method. At that point, the lock is free until some other thread enters a synchronized method on that object. Exiting synchronized code does not directly stop the execution of a thread.