troubleshooting Question

ExecutorService shutdownAndAwaitTermination()

Avatar of royjayd
royjayd asked on
2 Comments1 Solution1467 ViewsLast Modified:
hi guys

Was reading up the ExecutorService API

Was trying to understand what shutdownAndAwaitTermination() method does and
do we need to use it everytime we use ExecutroService?

Below is my scenario
executor.submit(myTask); --> i am submitting task to executor
shutdownAndAwaitTermination(executor); -- finally i am calling shutdownAndAwaitTermination()

//taken from java doc
shutdownAndAwaitTermination(ExecutorService pool)
void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown();//once the above task has been executed by threads,shut down executor ?
 try {
     // Wait 60 seconds  for existing tasks to terminate
     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {  //--what does this do?    
     pool.shutdownNow(); // Cancel currently executing tasks
       if (!pool.awaitTermination(60, TimeUnit.SECONDS))
           System.err.println("Pool did not terminate");
   } catch (InterruptedException ie) {
     // (Re-)Cancel if current thread also interrupted
     Thread.currentThread().interrupt();--what does this do?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros