一个 threadPoolExecutor 与 'Busy'线程被杀死? [英] How a threadPoolExecutor with 'Busy' threads is killed?

查看:47
本文介绍了一个 threadPoolExecutor 与 'Busy'线程被杀死?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有点复杂.让我尝试彻底解释它,但是如果您需要更多详细信息,请随时问我,我会添加它们.

My question is slight complicated. Let me try to explain it thoroughly, but if you need more details, feel free to ask me, I will add them.

我最近(通过实验)了解到,如果一个线程连续工作,比如 while(true) 循环中的整数运算,中断线程对其没有影响.话题像什么都没发生一样继续.

I learnt recently (by experiment) that if a thread is continuously doing work, something like an integer operation in a while(true) loop, interrupting the thread has no effect on it. Thread goes on like nothing happened.

现在,ThreadPoolExecutor 使用 shutdown() 或 shutdownNow() 方法被终止.我检查了这些方法的代码,它们使用 interrupt() 调用来终止线程.那么,如果所有线程都忙于做事,那么执行程序将如何被杀死?它是如何被杀死的,例如我们在 spring 应用程序中使用它时.

Now, ThreadPoolExecutors are killed using shutDown() or shutDownNow() methods. I checked the code for these methods, they use interrupt() call to kill a thread. So, if all threads are busy doing stuff, how would an executor be killed? How is it killed, for example when we use it in spring apps.

一个这样的执行者看起来像:

One such executor will look like:

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
    public static void main(String[] a) {
        ExecutorService ex = Executors.newFixedThreadPool(50);
        for (int i = 0; i < 50; i++) {
            ex.execute(new Thread() {
                public void run() {
                    try {
                        File f = File.createTempFile("testfile", "txt");
                        while (true) {
                            f.canRead();
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                    }
                }
            });
        }

        ex.shutdown();
        ex.shutdownNow();
    }
}

推荐答案

要回答你的主要问题,它不会,除非通过 System.exit().

To answer your main question, it wouldn't be, not unless the JVM was told explicitly to exit via System.exit().

对于应该可中断的长时间运行的操作,实现有责任通过Thread.currentThread().isInterrupted()检查中断标志.

For long running operations which should be interruptable, it is the responsibility of the implementation to check the interrupt flag via Thread.currentThread().isInterrupted().

例如:

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
    public static void main(String[] a) {
        ExecutorService ex = Executors.newFixedThreadPool(50);
        for (int i = 0; i < 50; i++) {
            ex.execute(new Runnable() {
                public void run() {
                    try {
                        File f = File.createTempFile("testfile", "txt");
                        while (!Thread.currentThread().isInterrupted()) {
                            f.canRead();
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                    }
                }
            });
        }

        ex.shutdown();
        ex.shutdownNow();
    }
}

另请注意,创建 Thread 的实例仅用作 Runnable 是不合适的.这样做会产生一种间接性,可能会使更多天真的程序员感到困惑.

Also note that it is not proper to create an instance of Thread for use solely as a Runnable. Doing so creates a sort of indirection that could confuse more naive programmers.

这篇关于一个 threadPoolExecutor 与 &amp;#39;Busy&amp;#39;线程被杀死?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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