从 wait() 中检查中断状态

Check interrupted status from within wait()

当线程 t 调用对象 u 上的 wait() 方法时,它会进入 WAITING 状态,直到另一个线程在同一对象上调用 notify() u 另一个线程调用等待线程 t 上的 interrupt() 方法。考虑到等待线程不消耗 CPU 周期,等待线程如何检查 wait() 内的 中断状态 并抛出 InterruptedException?

也就是说,我想象下面的代码在wait()内:

if (Thread.interrupted())  // Clears interrupted status!
  throw new InterruptedException();

必须恢复处于 WAITING 状态的线程才能再次变为 RUNNABLE。

此恢复在较低级别完成,并由通知或中断两者调用。

等待线程不执行任何代码。相反,中断标志被设置(在较低级别),线程被恢复 - 状态 RUNNABLE - 一旦它再次成为 运行,检查中断状态。