简体   繁体   English

具有'忙'线程的threadPoolExecutor如何被杀死?

[英]How a threadPoolExecutor with 'Busy' threads is killed?

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. 让我试着彻底解释一下,但是如果你需要更多的细节,请随时问我,我会添加它们。

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. 我最近(通过实验)了解到,如果一个线程持续工作,就像一个while(true)循环中的整数运算,中断线程对它没有影响。 Thread goes on like nothing happened. 线程继续没有发生任何事情。

Now, ThreadPoolExecutors are killed using shutDown() or shutDownNow() methods. 现在,使用shutDown()或shutDownNow()方法杀死ThreadPoolExecutors。 I checked the code for these methods, they use interrupt() call to kill a thread. 我检查了这些方法的代码,他们使用interrupt()调用来杀死一个线程。 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. 它是如何被杀死的,例如当我们在spring应用程序中使用它时。

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

To answer your main question, it wouldn't be, not unless the JVM was told explicitly to exit via System.exit() . 要回答你的主要问题,除非明确告知JVM通过System.exit()退出,否则不会。

For long running operations which should be interruptable, it is the responsibility of the implementation to check the interrupt flag via Thread.currentThread().isInterrupted() . 对于应该可中断的长时间运行操作,实现的责任是通过Thread.currentThread().isInterrupted()检查中断标志。

For example: 例如:

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

Also note that it is not proper to create an instance of Thread for use solely as a Runnable . 另请注意,创建Thread实例仅用作RunnableRunnable Doing so creates a sort of indirection that could confuse more naive programmers. 这样做会产生一种间接性,可能会让更多天真的程序员感到困惑。

You shouldn't be having a infinate loop in your code. 您不应该在代码中使用infinate循环。

The Thread Pool will does not wait for the threads to exit. 线程池不会等待线程退出。

The thread will run forever until the JVM exits 该线程将永远运行,直到JVM退出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM