为什么';Java线程接收中断标志?
我试图理解ExecutorService中的中断线程,但我不明白为什么下面的MyNeverEndingRunnable类没有得到中断信号。我有一个实现Runnable的类,它只是在循环中打印和等待,直到被中断:为什么';Java线程接收中断标志?,java,multithreading,interrupt,executorservice,threadpoolexecutor,Java,Multithreading,Interrupt,Executorservice,Threadpoolexecutor,我试图理解ExecutorService中的中断线程,但我不明白为什么下面的MyNeverEndingRunnable类没有得到中断信号。我有一个实现Runnable的类,它只是在循环中打印和等待,直到被中断: class MyNeverEndingRunnable implements Runnable { int count = 0; @Override public void run() { while (true)
class MyNeverEndingRunnable
implements Runnable
{
int count = 0;
@Override
public void run()
{
while (true)
{
System.out.printf("[%d]:%d\n", Thread.currentThread().getId(), ++count);
try { Thread.sleep(5000L); } catch (Exception ignored) {}
if (Thread.interrupted())
{
break;
}
}
}
}
我生成了一些线程,然后在我的ExecutorService上调用shutdownNow(),它应该在每个正在运行的线程上调用interrupt,但下面的代码将永远继续运行:
int threadCount = 5;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
Future[] threads = new Future[threadCount];
for (int k = 0; k < threadCount; ++k)
{
threads[k] = executorService.submit(new MyNeverEndingRunnable());
}
Thread.sleep(20000L);
executorService.shutdownNow();
while (!executorService.isShutdown()) Thread.sleep(1000L);
int threadCount=5;
ExecutorService ExecutorService=Executors.newFixedThreadPool(线程计数);
Future[]threads=新的Future[threadCount];
用于(int k=0;k
有人知道我做错了什么吗?来自:
InterruptedException
-如果任何线程中断了当前线程。引发此异常时,当前线程的中断状态将被清除。[我的重点]
注意:实际上没有任何东西可以保证线程被shutdownNow()
中断。它只是将其描述为“典型实现”
你的代码有点奇怪。试试这个:
try
{
Thread.sleep(5000L);
}
catch (InterruptedException exc)
{
break;
}
并从以下位置移除线程.interrupted()
测试。:
InterruptedException
-如果任何线程中断了当前线程。引发此异常时,当前线程的中断状态将被清除。[我的重点]
注意:实际上没有任何东西可以保证线程被shutdownNow()
中断。它只是将其描述为“典型实现”
你的代码有点奇怪。试试这个:
try
{
Thread.sleep(5000L);
}
catch (InterruptedException exc)
{
break;
}
然后移除线程.interrupted()
测试。阅读:
抛出:
...
InterruptedException-如果任何线程中断了当前线程。引发此异常时,当前线程的中断状态将被清除
一旦抛出异常,它就不再被中断。在您的情况下,您可以像@EJP建议的那样,立即跳出循环,让线程消亡。但是,如果您的代码没有线程的所有权(例如,一个单独的方法),您需要通过传播异常或重新中断来确保将中断传播到调用方:
try {
while (true) {
System.out.printf("[%d]:%d\n", Thread.currentThread().getId(), ++count);
Thread.sleep(5000L);
}
} catch (InterruptedException notIgnored)
Thread.currentThread().interrupt();
}
或类似地:
while (!Thread.currentThread().isInterrupted()) {
System.out.printf("[%d]:%d\n", Thread.currentThread().getId(), ++count);
try {
Thread.sleep(5000L);
} catch (InterruptedException notIgnored)
Thread.currentThread().interrupt();
}
}
阅读:
抛出:
...
InterruptedException-如果任何线程中断了当前线程。引发此异常时,当前线程的中断状态将被清除
一旦抛出异常,它就不再被中断。在您的情况下,您可以像@EJP建议的那样,立即跳出循环,让线程消亡。但是,如果您的代码没有线程的所有权(例如,一个单独的方法),您需要通过传播异常或重新中断来确保将中断传播到调用方:
try {
while (true) {
System.out.printf("[%d]:%d\n", Thread.currentThread().getId(), ++count);
Thread.sleep(5000L);
}
} catch (InterruptedException notIgnored)
Thread.currentThread().interrupt();
}
或类似地:
while (!Thread.currentThread().isInterrupted()) {
System.out.printf("[%d]:%d\n", Thread.currentThread().getId(), ++count);
try {
Thread.sleep(5000L);
} catch (InterruptedException notIgnored)
Thread.currentThread().interrupt();
}
}
Thread#isInterrupted()
不是静态方法。该代码如何编译?为什么在睡眠中忽略Runnable的异常?噢,天哪,你不是在调用isInterrupted()
@hovercraftfullofels一种方法是如果代码不调用该方法。Thread#isInterrupted()
不是静态方法。该代码如何编译?为什么在睡眠中忽略Runnable的异常?噢,天哪,你不是在调用isInterrupted()
@HoverCraftFullOfels一种方法是如果代码不调用该方法。是的,这就是问题所在,我捕获并忽略了InterruptedException。谢谢是的,这就是问题所在,我捕获并忽略了中断异常。谢谢一旦抛出异常,它就不再被中断。与“和捕获”无关。只要抛出异常,它就不再被中断。与“被抓住”无关。