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