How to stop all runnable thread in java executor class?
Solution 1
The shutDown()
method simply prevents additional tasks from being scheduled. Instead, you could call shutDownNow()
and check for thread interruption in your Runnable
.
// in your Runnable...
if (Thread.interrupted()) {
// Executor has probably asked us to stop
}
An example, based on your code, might be:
final ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(new Runnable() {
public void run() {
try {
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
System.out.println("Interrupted, so exiting.");
}
}
});
if (executor.awaitTermination(10, TimeUnit.SECONDS)) {
System.out.println("task completed");
} else {
System.out.println("Forcing shutdown...");
executor.shutdownNow();
}
Solution 2
It is generally a bad idea to terminate a running thread from the outside, because you don't know the state the thread is currently in. It's possible that it needs to do some cleanups, and it won't be able to do that when you forcefully shut it down. That's why all methods of Thread which do that are marked as deprecated.
It's much better to use one of the many techniques which are available for interprocess communication to signal the procedure running in the thread itself that it has to abort its work and exit normally. One way to do this is to add an abort()
method to your runnable, which raises a flag declared as volatile
. The inner loop of your Runnable checks that flag and exits (in a controlled fashion) when that flag is raised.
Comments
-
Amit Pal almost 2 years
final ExecutorService executor = Executors.newFixedThreadPool(1); final Future<?> future = executor.submit(myRunnable); executor.shutdown(); if(executor.awaitTermination(10, TimeUnit.SECONDS)) { System.out.println("task completed"); }else{ System.out.println("Executor is shutdown now"); } //MyRunnable method is defined as task which I want to execute in a different thread.
Here is
run
method of executor class:public void run() { try { Thread.sleep(20 * 1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }}
Here it is waiting for
20
second but when i run the code it throws an exception:java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method)
I am not able to close the concurrent thread ruining in
Java Executor class
. Here is my Code flow:- Created a new Thread with Java executor class to run some task i.e written in
MyRunnable
executor
wait for 10 second to complete the tasks.- If the task has completed then runnable thread also got terminated.
- If the task is not completed within 10 second then
executor
class should terminate the thread.
Everything works fine except the termination of tasks in the last scenario. How should I do it?
- Created a new Thread with Java executor class to run some task i.e written in
-
CodeChimp about 11 yearsI was JUST about to say this. This is the reason why Thread.stop() is depricated: docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()
-
Amit Pal about 11 yearsPlease see the run method in question. I tried it with but throws an exception
-
Duncan Jones about 11 yearsSee my edit above. This code sample will wait for ten seconds, then force a shutdown.
-
Duncan Jones about 11 yearsIn the specific case of an
ExecutorService
, I would vote for supporting thread interruption rather than a flag. In many frameworks, the service will be terminated withshutdownNow()
. -
Amit Pal about 11 years@Ducan Exactly i tried it with the above code but it throws mw an exception which i wrote in my question
-
Duncan Jones about 11 yearsAn exception is thrown because you've chosen to use a blocking method (
sleep()
) in your example code. That is the correct, expected behaviour. -
fl4l almost 10 yearsCorrect! Is important to check
if (Thread.interrupted())
in Runnable class, otherwise thread cotinues to run -
jocull over 2 yearsDoes this not need a call to
shutdown
/shutdownNow
beforeawaitTermination
?