开发者

How a threadPoolExecutor with 'Busy' threads is killed?

My question is slight 开发者_如何学Pythoncomplicated. 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. Thread goes on like nothing happened.

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


To answer your main question, it wouldn't be, not unless the JVM was told explicitly to exit via 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().

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. Doing so creates a sort of indirection that could confuse more naive programmers.


You shouldn't be having a infinate loop in your code.

The Thread Pool will does not wait for the threads to exit.

The thread will run forever until the JVM exits

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜