Solved

ExecutorService  shutdownAndAwaitTermination()

Posted on 2012-03-15
2
1,120 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
ID: 37727588
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
ID: 37763911
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

895 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

12 Experts available now in Live!

Get 1:1 Help Now