Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ExecutorService - What if awaitTermination() fails?

If I have the following code, which works fine:

ExecutorService service = Executors.newFixedThreadPool(100);

[....]

List<Future<SomeObject>> futures = service.invokeAll(callables);
for (Future f : futures) {
    f.get();
}

// shutdown the service after all Callables are finished.
service.shutdown();

boolean serviceIsShutDown = service.awaitTermination(5, TimeUnit.SECONDS);
if (serviceIsShutDown) {
    System.out.println("Service terminated normally. All ok.");
} else  {
    // What if it's not shutDown?
    [...]
    // this? 
    //service = null;
}   

Question: What if the call

boolean serviceIsShutDown = service.awaitTermination(5, TimeUnit.SECONDS);

returns false because the timeout hits?

I guess the Threads in the ExecutorService will remain in state WAIT. What is the best solution to continue? Setting the service to null and having the GarbageCollector remove it? But what happens with the related Threads? Will it ever be garbage collected as there are still references?

The code usually works, but just be curious. What to do if it returns false?

like image 290
Joschi Avatar asked Sep 09 '16 14:09

Joschi


2 Answers

If awaitTermination() returns false in your example, you have an option to try calling shutdownNow(). This method will do its best to cancel all the tasks that are still being executed, but it guarantees nothing. Some poorly implemented tasks might have no cancellation policy and just run forever. In this case, the threads will never be terminated and the executor will never be garbage collected.

Such tasks will also prevent your program from graceful termination (if you don't mark your working threads as daemons).

For instance, if your task only contains an empty infinite loop, it won't be cancelled even if you call shutdownNow().

There also might be the case that a task has no proper cancellation policy, and runs too long (but not forever). For instance, it has a very-very long empty loop. You might fail to shutdown a pool that is being executed such task by means of shutdown()/shutdownNow(), but sooner or later it will finish its work and the thread will be terminated along with the executor.

like image 171
Andrew Lygin Avatar answered Nov 08 '22 09:11

Andrew Lygin


If you want to "force" the termination of your ExecutorService just use:

shutdownNow()

look here for description:

enter image description here

like image 27
Moshe Arad Avatar answered Nov 08 '22 10:11

Moshe Arad