线程interrupt()不中断线程 [英] Thread interrupt() does not interrupt thread

查看:107
本文介绍了线程interrupt()不中断线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,当遍历线程列表并中断所有线程时,它们都不执行带有InterruptedException的catch块.

For some reason, when iterating over a list of threads and interrupting all of them, none of them executes the catch block with InterruptedException.

在以下存储库中: https://github.com/ranisalt/LiuThread

我有两个线程,每个线程都有执行器服务,其中一个线程拥有读者,另一个线程则是作家.编写器应每100毫秒产生一次,写入缓冲区,然后退出/停止/无论什么都不要再次写入.读者是4个尝试阅读的线程,如果可以,他们也应该退出,因此还有其他读者可以使用的空间.

I have two threads, each of them has executor services and one of them holds readers, the other reads writers. Writers should spawn every 100 ms, write to the buffer and quit/stop/whatever just don't write again. Readers are 4 threads that try to read and, if they can, they should quit too, so there's space for another reader.

我每个人(读卡器和写卡器)有100个,如果他们不能读或写,他们将等待60秒.

I have 100 of each (reader and writer) and if they fail to read or write, they wait 60 seconds.

然后,我有一个协调器线程,该线程一直在测试缓冲区是否为空(并中断编写器)或已满(中断读取器),因此它们永远不应等待60秒钟.缓冲区知道其状态.

Then, I have a coordinator thread that keeps testing if the buffer is empty (and interrupts the writers) or full (interrupts the readers), so they never should get to wait 60s. The buffer knows its state.

问题是由于某些原因而造成的:

The problem is, for some reason, this snippet:

for (ThreadAzul azul : threadsAzuis) {
    azul.interrupt();
}

不中断线程!我已经用println查看是否被打断了:

Is NOT interrupting the thread! I have used println to see if is getting interrupted:

try {
    sleep(60000);
    System.out.println(this.getName() + " foi dormir");
} catch (InterruptedException e) {
    System.out.println(this.getName() + " foi interrompido");
}

但是,这从来没有写过.中断()无法使catch块执行的原因是什么?

But this NEVER gets written. What is the reason for interrupt() not making the catch block execute?

推荐答案

main 方法内部,您正在 Thread run()>,所以您永远不会启动新的 Thread ,而是在初始的 main 线程中运行其代码.因此,在从未启动过的 Thread 实例上调用 interrupt 不会中断实际上正在执行代码的 main 线程.

Inside the main method you are invoking run() on your Thread so you never start a new Thread but instead run its code within the initial main thread. So invoking interrupt on the Thread instance you never have started will not interrupt the main thread which is actually executing the code.

Thread 实例提交给 Executor 时,重复同样的错误.执行者将执行 Thread 实例的 run 方法,因为 Thread 实现了 Runnable Executor 将在其自己的托管 Thread 中执行此操作,但不会在您已创建但从未启动的实例代表的 Thread 中执行该操作.

The same error repeats when you submit a Thread instance to an Executor. The executor will execute the run method of the Thread instance because Thread implements Runnable but the Executor will do so within its own managed Thread but not within the Thread represented by the instance you have created but never started.

同样,在从未启动过的 Thread 实例上调用 interrupt 不会中断实际执行代码的线程.

So again, invoking interrupt on the Thread instance you never have started will not interrupt the thread actually executing the code.

通常,您不应该不要混用 线程 Executor .确定一种方式,手动处理 Thread 或使用 ExecutorSevice s.

Generally you should not mix Thread and Executor usage. Decide for one way, dealing with Threads manually or using ExecutorSevices.

当使用 Thread 时,必须使用 start()方法启动它们,而不是调用 run()方法.

And when you are using Threads you must start them using the start() method, not invoking the run() method.

这篇关于线程interrupt()不中断线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆