Jay Roy
asked on
ExecutorService shutdownAndAwaitTermination()
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 shutdownAndAwaitTerminatio n() 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
...
shutdownAndAwaitTerminatio n(executor ); -- finally i am calling shutdownAndAwaitTerminatio n()
...
...
//taken from java doc
shutdownAndAwaitTerminatio n(Executor Service pool)
void shutdownAndAwaitTerminatio n(Executor Service 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().int errupt();- -what does this do?
}
}
Was reading up the ExecutorService API
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html
Was trying to understand what shutdownAndAwaitTerminatio
do we need to use it everytime we use ExecutroService?
Below is my scenario
...
...
executor.submit(myTask); --> i am submitting task to executor
...
shutdownAndAwaitTerminatio
...
...
//taken from java doc
shutdownAndAwaitTerminatio
void shutdownAndAwaitTerminatio
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
pool.shutdownNow(); // Cancel currently executing tasks
if (!pool.awaitTermination(60
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
Thread.currentThread().int
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
shutdownAndAwaitTerminatio
void shutdownAndAwaitTerminatio
pool.shutdown();
try {
if (!pool.awaitTermination(60
pool.shutdownNow();
if (!pool.awaitTermination(60
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
Thread.currentThread().int
}
}
If you can tell me what happens step - by - step that will be great.
thanks