如何杀死具有“繁忙"线程的threadPoolExecutor? [英] How a threadPoolExecutor with 'Busy' threads is killed?

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

问题描述

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

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.

现在,使用shutdownDown()或shutDownNow()方法杀死ThreadPoolExecutor.我检查了这些方法的代码,它们使用interrupt()调用杀死线程.那么,如果所有线程都在忙于做某事,那么执行者将如何被杀死?它是如何被杀死的,例如,当我们在春季应用中使用它时.

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();
    }
}

推荐答案

要回答您的主要问题,不是,除非明确告知JVM通过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();
    }
}

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

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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