Android isInterrupted() returns 中断线程后为 false
Android isInterrupted() returns false after interrupting Thread
我有一个线程等待通知或超时
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()
在此之后仍然 returns 为真并且 while 循环继续 运行.
这是为什么?
您可以在 while 循环条件项中使用布尔值,因为当您发送中断命令时,它不会停止线程,而线程本身决定何时停止自身
我有一个线程等待通知或超时
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()
在此之后仍然 returns 为真并且 while 循环继续 运行.
这是为什么?
您可以在 while 循环条件项中使用布尔值,因为当您发送中断命令时,它不会停止线程,而线程本身决定何时停止自身