1

When a thread t calls the wait() method on an object u, it goes to the WAITING state until another thread calls notify() on the same object u OR another thread calls the interrupt() method on the waiting thread t. Taking into account that a waiting thread does not consume CPU cycles, how is it possible for the waiting thread to check the interrupted status within wait() and throw an InterruptedException?

That is, I imagine the following code within wait():

if (Thread.interrupted())  // Clears interrupted status!
  throw new InterruptedException();
Athanasios V.
  • 319
  • 1
  • 4
  • 14

1 Answers1

4

A thread in the status WAITING must be resumed to become RUNNABLE again.

This resumption is being done at a lower level and is invoked by both, a notification or an interrupt.

A waiting thread does not execute any code. Instead the interrupt flag is set (at a lower level), the thread is resumed - state RUNNABLE - and once it is RUNNING again, the interrupt status is checked.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66