清除 Thread.interrupt() 标志的方法

2022-09-01 16:33:34

我最近继承了一个大型Java应用程序,它几乎没有线程安全。我目前正在做的是让所有的线程正确处理被中断,而不是使用非常糟糕的。Thread.stop()

部分问题在于我不知道清除中断标志的每个方法调用。

目前我知道以下内容将清除中断标志:

Thread.interrupted()
Thread.sleep(long)
Thread.join()
Thread.join(long)
Object.wait()
Object.wait(long)

我还错过了什么?谢谢


答案 1

部分问题在于我不知道清除中断标志的每个方法调用。

重要的是要澄清以下方法通过调用它们来清除中断标志:

Thread.interrupted()
Thread.isInterrupted(true) -- added to your list

因此,应始终使用。Thread.currentThread().isInterrupted()

以下方法将通过立即抛出中断标志来清除中断标志,如果它们被调用,然后线程被中断,或者如果线程已经中断,然后它们被调用(请参阅下面的 junit 代码)。因此,清除标志的方法不是,而是引发异常。InterruptedException

您的初始列表:

Thread.interrupted()
Thread.sleep(long)
Thread.join()
Thread.join(long)
Object.wait()
Object.wait(long)

已添加到您的列表中:

Thread.sleep(long, int)
Thread.join(int, long)
Thread.isInterrupted(true)
Object.wait(int, long)
BlockingQueue.put(...)
BlockingQueue.offer(...)
BlockingQueue.take(...)
BlockingQueue.poll(...)
Future.get(...)
Process.waitFor()
ExecutorService.invokeAll(...)
ExecutorService.invokeAny(...)
ExecutorService.awaitTermination(...)
CompletionService.poll(...)
CompletionService.take(...)
CountDownLatch.await(...)
CyclicBarrier.await(...)
Semaphore.acquire(...)
Semaphore.tryAcquire(...)
Lock.lockInteruptibly()
Lock.tryLock(...)

请注意,对于任何捕获的代码,正确的模式是立即重新中断线程。我们这样做是为了以防其他人依赖该方法:InterruptedExceptionthread.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());

答案 2

常见的约定如下:任何抛出 (+) 的方法都会清除中断标志。InterruptedExceptionThread.interrupted()

因此,为了使线程可中断,您需要找到所有被捕获的地方,而无需回溯它或恢复中断标志。由于是已检查的异常,因此不难做到。InterruptedExceptionInterruptedException