問題の一部は、割り込みフラグをクリアするすべてのメソッド呼び出しがわからないということでした。
以下の方法がちょうどそれらを呼び出すことにより、割り込みフラグをクリアすることを明らかにすることが重要である。このような理由のためにThread.currentThread().isInterrupted()
Thread.interrupted()
Thread.isInterrupted(true) -- added to your list
は常に代わりに使用する必要があります。
以下の方法が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
をキャッチ任意のコードとの適切なパターン。これのいくつかを示してい
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の代わりに一般的なExceptionをキャッチする状況に直面しています。 – OverflowingStack