Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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();
    }
}
like image 478
cheekoo Avatar asked Jan 13 '11 01:01

cheekoo


1 Answers

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.

like image 147
Tim Bender Avatar answered Sep 20 '22 23:09

Tim Bender