Solved

ExecutorService  shutdownAndAwaitTermination()

Posted on 2012-03-15
2
1,193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

617 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