Android isInterrupted()在中断线程后返回false



我有一个线程正在等待通知或超时

while (!isInterrupted()) {
try {
if (!semaphore.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
Context context = getContext();
// this is where i do my stuff
semaphore.aquire();
}
} catch (InterruptedException ignored) {
ignored.printStackTrace();
}
}

在我退出我的应用程序之前,我在这个线程上调用Thread#interrupt()。现在我调试了一下,中断似乎执行得很好,因为InterruptedException被抛出,等待被取消了
但由于某种原因,isInterrupted()在此之后仍然返回true,while循环继续运行。

为什么

您可以在while循环条件项中使用布尔值,因为当您发送中断命令时,它不会停止线程,线程自己决定何时停止

最新更新