4

I have to wait for completion of all the threads in an executorService. Should I use

while(!executor.isTerminated()){...} or 
executor.awaitTermination(...)?

What are the pros and cons of these ways?

Swaraj Shekhar
  • 187
  • 1
  • 7
  • 28
  • 1
    IMHO, why not you go with Future and wait for task to complete. – PrabaharanKathiresan Jan 09 '17 at 06:45
  • I need not get back any result, so was planning not use Future. – Swaraj Shekhar Jan 10 '17 at 06:22
  • That’s the fundamental difference between [Busy waiting](https://en.wikipedia.org/wiki/Busy_waiting) and using [dedicated wait functions](https://en.wikipedia.org/wiki/Busy_waiting#Alternatives). The answer doesn’t depend on the actual condition, i.e. termination of the executor service, at all. – Holger Jan 11 '17 at 10:13

2 Answers2

3

With executor.isTerminated() your current thread will keep running. with executor.awaitTermination() the current thread will be blocked. So it depends on what you want to do in your current thread. Do you want to do some tasks and periodically check for whether the executor is done, then use executor.isTerminated()? Or Is the current thread just waiting for the executor to finish. If yes, executor.awaitTermination() makes much more sense.

Do note that an Executor will only be terminated if the shutdown() or shutdownNow() is called.

Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
  • No checks but only waiting for executor to finish. Is there any advantages/disadvantages of one over another? – Swaraj Shekhar Jan 10 '17 at 06:23
  • with `isTerminated()` you need to manage the current threads sleep whereas with `awaitTermination()` you could put a big enough timeout and just have the current thread block. – Yogesh_D Jan 10 '17 at 07:24
3

Recommended way from oracle documentation link:

void shutdownAndAwaitTermination(ExecutorService pool) {
   pool.shutdown(); // Disable new tasks from being submitted
   try {
     // Wait a while for existing tasks to terminate
     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
       pool.shutdownNow(); // Cancel currently executing tasks
       // Wait a while for tasks to respond to being cancelled
       if (!pool.awaitTermination(60, TimeUnit.SECONDS))
           System.err.println("Pool did not terminate");
     }
   } catch (InterruptedException ie) {
     // (Re-)Cancel if current thread also interrupted
     pool.shutdownNow();
     // Preserve interrupt status
     Thread.currentThread().interrupt();
   }
 }

if your threads are not completing with-in 120 seconds, you can change second if condition as :

while(!pool.awaitTermination(60, TimeUnit.SECONDS)) {
     Thread.sleep(60000);
}  

You can find other alternatives at @ wait until all threads finish their work in java

One more key note on usage of isTerminated:

boolean isTerminated()

Returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • I am using Runnable in ExecutorService. Will there be a difference in Exception Handling while using isTerminated() vs awaitTermination()? Can I handle exception in this case? – Swaraj Shekhar Jan 10 '17 at 06:25
  • If you call execute method instead of submit method of executorservice, it is ok. – Ravindra babu Jan 10 '17 at 06:38