我很难理解Java线程中断标志的语义。我的理解是,只有在线程被中断之后,标志才应该是true,一旦设置为true,则在捕获InterruptedException或等效标记或使用.interrupted()显式清除标志之前,不再为false。因此,我无法解释为什么下面的程序会打印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()抛出异常是可以的。)
发布于 2014-01-19 06:16:32
1)开放CyclicBarrier源代码,您将看到await()方法通过调用Thread.currentThread().interrupt()重置中断标志。请参见doWait(),它是wait()的实际推动力。实际上,CyclicBarrier的javadoc就是这么说的。
2)我不同意在InterruptedException被捕获时清除中断标志
https://stackoverflow.com/questions/21213783
复制相似问题