0

I have the code below called from main thread, using ExecutorService pool and starting a thread to process each file found. I am trying to understand the behaviour of ExecutorService when the main thread gets terminated by a kill command. What happens to the spawned threads ? Do they immediately get killed or they terminate once they've finished their job ?

Also is there any better/safer way to write the below snippet, especially if I am to run this part in an infinite loop , eg waiting for files to be dropped to the input dir and assign threads to process them? In that case should I be creating a new Pool and .awaitTermination in each loop iteration ?

Many thanks

ExecutorService executorService = Executors.newFixedThreadPool(maxThreads);

        for (File inputFile : inputDir.listFiles()) {   
        if (inputFile.isFile())     
                executorService.submit(new MyRunnable(inputFile));      
        }

        executorService.shutdown();
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
2
  • Do you mean once your are killing the JVM process? Commented Oct 19, 2018 at 7:45
  • If you kill JVM process, All threads will get terminated in the process. Thread always run in the scope of process. Commented Oct 19, 2018 at 7:52

2 Answers 2

2

when the main thread gets terminated by a kill command. What happens to the spawned threads?

The main thread is not killed (if you mean kill <pid> from the command-line), but the JVM is killed in which case the kill affects all threads running. You can setup shutdown handlers that will be triggered if a kill signal (not kill -9) is received. See: https://stackoverflow.com/a/2541618/179850

In that case should I be creating a new Pool and .awaitTermination in each loop iteration ?

No. What I would do is submit your jobs to the thread-pool started outside of the loop but keep the Futures returned by executorService.submit(...) in a collection inside of the loop. The you can do something like the following to wait for each of the jobs to finish. There are exceptions for you to handle here:

// this is all inside of the loop with the executorService created outside of loop
List<Future<?>> futures = new ArrayList<>();
for (File inputFile : inputDir.listFiles()) {   
    if (inputFile.isFile())     
         futures.add(executorService.submit(new MyRunnable(inputFile)));      
    }
}
// now we go back and wait for the jobs to finish
for (Future<?> future : futures) {
    // this waits for each job to finish
    // it throws some exceptions that you'll need to catch and handle
    future.get();
}
1

If the main thread is somewhat done, crashed, sleep... that does not affect the other threads as long as your logic is not coupled with it. They are independent and they will keep on doing the work for you (as long as they are not demon threads which are not prioritized by the JVM and the process will be complete of only demon threads are left). You maintained:

terminated by a kill command

and if that means to kill the PID / kill the JVM process then yes all of the threads will be shut down with the application itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.