我在理解 Java 线程中断标志的语义时遇到了一些麻烦。我的理解是,该标志只应在线程中断后为真,一旦设置为真,InterruptedException
在捕获或等效物或用.interrupted()
. 因此,我无法解释为什么会打印以下程序false
:
Thread t = new Thread() {
@Override
public void run() {
try {
// While await()ing, another thread calls t.interrupt().
new CyclicBarrier(2).await();
} finally {
// I believe I should still be interrupted here, but am not...
System.out.println(Thread.currentThread().isInterrupted());
}
}
};
(为简单起见排除了一些细节 - 假设可以run()
抛出异常。)