Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the execution of Executor ThreadPool in java?

I am working on the Executors in java to concurrently run more threads at a time. I have a set of Runnable Objects and i assign it to the Exceutors.The Executor is working fine and every thing is fine.But after all the tasks are executed in the pool ,the java program is not terminated,i think the Executor takes some time to kill the threads.please anyone help me to reduce the time taken by the executor after executing all tasks.

like image 845
Rajapandian Avatar asked Oct 13 '09 18:10

Rajapandian


People also ask

Which method can be used to stop the Threadpool executor?

When using an Executor, we can shut it down by calling the shutdown() or shutdownNow() methods. Although, it won't wait until all threads stop executing. Waiting for existing threads to complete their execution can be achieved by using the awaitTermination() method.

How do you stop Threadpool?

shutdownNow() should be used to shutdown the thread pool to gracefully exiting the application.

How do I stop scheduled executor service?

Learn to cancel a task submitted to an executor service if the task still has to be executed and/or has not been completed yet. We can use the cancel() method of Future object that allows making the cancellation requests.

How do you stop a runnable in Java?

You can call cancel() on the returned Future to stop your Runnable task.


1 Answers

The ExecutorService class has 2 methods just for this: shutdown() and shutdownNow().

After using the shutdown() method, you can call awaitTermination() to block until all of the started tasks have completed. You can even provide a timeout to prevent waiting forever.

You might want to click on some of these links that I'm providing. They go straight to the docs where you can readup on this stuff yourself.

like image 90
Outlaw Programmer Avatar answered Dec 03 '22 21:12

Outlaw Programmer