问题的一部分是,我不知道每个方法调用那里清除中断标志。
必须明确指出,通过下列方法只是称他们清除中断标志是很重要的:
Thread.interrupted()
Thread.isInterrupted(true) -- added to your list
为此Thread.currentThread().isInterrupted()
应该总是代替。
下面的方法将立即扔InterruptedException
清除中断标志或者如果他们被称为然后线程被中断或如果线程已经打断了,然后他们被称为(见下面的JUnit代码)。所以它不是清除标志的方法,抛出异常。
Thread.sleep(long)
Thread.join()
Thread.join(long)
Thread.join(int, long) – added to your list
Object.wait()
Object.wait(long)
Object.wait(int, long) – added to your list
BlockingQueue.put(...) – added to your list
BlockingQueue.offer(...) – added to your list
BlockingQueue.take(...) – added to your list
BlockingQueue.poll(...) – added to your list
Future.get(...) – added to your list
请注意与任何代码映入InterruptedException
是立即重新中断线程的正确模式。我们这样做的情况下,其他人都是靠thread.isInterrupted()
方法:
try {
...
} catch (InterruptedException e) {
// immediately re-interrupt the thread
Thread.currentThread().interrupt();
// log the exception or [likely] quit the thread
}
JUnit的代码,演示了一些这样的:
assertFalse(Thread.currentThread().isInterrupted());
// you can do this from another thread by saying: someThread.interrupt();
Thread.currentThread().interrupt();
// this method does _not_ clear the interrupt flag
assertTrue(Thread.currentThread().isInterrupted());
// but this one _does_ and should probably not be used
assertTrue(Thread.interrupted());
assertFalse(Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt();
assertTrue(Thread.currentThread().isInterrupted());
try {
// this throws immediately because the thread is _already_ interrupted
Thread.sleep(1);
fail("will never get here");
} catch (InterruptedException e) {
// and when the InterruptedException is throw, it clears the interrupt
assertFalse(Thread.currentThread().isInterrupted());
// we should re-interrupt the thread so other code can use interrupt status
Thread.currentThread().interrupt();
}
assertTrue(Thread.currentThread().isInterrupted());
这是第一次抛出我执行的代码库,但是我面临的情况是以前的程序员会捕获一般异常而不是InterruptedException。 – OverflowingStack