Solved

ExecutorService  shutdownAndAwaitTermination()

Posted on 2012-03-15
2
1,108 Views
Last Modified: 2012-08-14
hi guys

Was reading up the ExecutorService API
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

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
     pool.shutdownNow();
     Thread.currentThread().interrupt();--what does this do?
   }
 }
0
Comment
Question by:royjayd
2 Comments
 
LVL 1

Accepted Solution

by:
nareshvoota earned 350 total points
Comment Utility
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#interrupt()

pool.awaitTermination throws InterrupedException

If another thread called interrupt on this thread, pool.awaitTermination will throw Interrupted exception and clears the interrupted status on the thread.

so if you call

Thread.currentThread().interrupt() again, that preserves the interrupt status of that thread making sure the calling hierarchy knows that the thread is interrupted.

As an alternative, you can rethrow the interrupted exception instead of calling Thread.currentThread().interrupt()

Hope this helps.
0
 

Author Comment

by:royjayd
Comment Utility
thanks,
I find it little confusing.
You say
>>If another thread called interrupt on this thread, pool.awaitTermination will throw Interrupted exception and clears the interrupted status on the thread

static boolean interrupted() method clears the interrupted status and not the void interrupt() method, correct?

My question is in comments:

--taken from java doc
shutdownAndAwaitTermination(ExecutorService pool)
void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown();
 try {    
     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {  //this is a blocking method, so   an interrupted exception is thrown, correct?  
     pool.shutdownNow();        
       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();
     Thread.currentThread().interrupt();  // Most importantly i want to know what does this do?
   }
 }

If you can tell me what happens step - by - step that will be great.

thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
network + 7 73
stringclean challenge 26 55
compre toata in where clue oracle 4 41
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now