0

I have created a ExecutorService having 3 threads using which I'm trying to process a list each having 2MM objects. I'm calling below 2 methods after submitting the 3 tasks.

executorService.shutdown();
executorService.awaitTermination(20, TimeUnit.HOURS);

However, once the 3 threads start execution, the main thread terminates without waiting for the processing threads to complete execution. What am I missing?

ExecutorService executorService = new ThreadPoolExecutor(3, 3, 1L, TimeUnit.MINUTES, 
new LinkedBlockingQueue<>(BLOCKING_QUEUE_SIZE), new ThreadPoolExecutor.CallerRunsPolicy());

List<List<String>> listOfList = new ArrayList<>(); // Each list in this list has 2MM.
for (List<String> list : listOfList) {
    executorService.submit(() -> processList(list));
}

executorService.shutdown();            
final boolean awaitTermination = executorService.awaitTermination(20, TimeUnit.HOURS);
if (awaitTermination) {
    System.out.print("Await Termination returned True\n");
} else {
    System.out.print("Await Termination returned False\n");
}

System.out.printf("Terminating main thread!\n");

The processList method looks something like -

private void processList(List<String> list) {

final String uuid = UUID.randomUUID().toString();
System.out.printf("UUID - %s\n", uuid);

// Process the list. This takes about 1hr

System.out.printf("UUID - %s - Completed processing!\n", uuid);
}

Output -

UUID - 448553ca-93ba-459e-a864-a3f1bf2aadf1
UUID - da43f632-ff75-42d2-9244-bc3c1fb834ea
UUID - 9f028167-eef0-4017-903e-9246d8670dee
Await Termination returned True
Terminating main thread!
9
  • What JRE version are you using? The output looks like what I would expect if your program called shutdownNow instead of shutdown. The three worker threads each finished the tasks that they were working on, but then they took no more tasks from the queue. Seems plausible that it could be a bug in the JRE. Commented Aug 13, 2024 at 13:05
  • If it is a JRE bug, and if you are unable to obtain a fix, then here's an idea for a workaround: Instead of having your main thread shut the pool down, submit a special shutdown task, after all of the other tasks. Since the pool workers pick the tasks from the queue in strict FIFO order, it will be guaranteed that all of the real tasks have been started by the time the shutdown task is executed. Commented Aug 13, 2024 at 13:11
  • 2
    I couldn't reproduce this issue. It waits until all threads have completed, as expected. Please create a minimal, self-contained executable to show the problem.
    – k314159
    Commented Aug 13, 2024 at 13:57
  • 1
    Each list in this list has 2MM Pardon my ignorance, but what is 2MM ?
    – Abra
    Commented Aug 14, 2024 at 6:03
  • 1
    It likely that your submitted tasks are throwing RuntimeException and so none are running when main thread calls awaitTermination and so it exits without seeing any "Completed processing". As you don't keep track of any Future you aren't determining whether these tasks finished normally or with expection.
    – DuncG
    Commented Aug 20, 2024 at 20:44

0