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.
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.
shutdownNow() should be used to shutdown the thread pool to gracefully exiting the application.
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.
You can call cancel() on the returned Future to stop your Runnable task.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With