将java.util.concurrent.locks.AbstractQueuedSynchronizer 中的以下代码视为标题中提出的想法的众多示例之一:
1258 public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout) throws Interrupted Exception {
1259 if (Thread.interrupted())
1260 throw new InterruptedException();
1261 return tryAcquireShared(arg) >= 0 ||
1262 doAcquireSharedNanos(arg, nanosTimeout);
1263 }
为什么大多数阻塞库方法通过调用静态方法Thread.interrupted()
而不是实例方法来响应中断isInterrupted()
。调用静态方法也会清除中断状态,所以即使任务处理代码吞下了 InterruptionException ,调用者也无法了解中断状态,除了