java中如何停止Executor ThreadPool的执行? [英] How to stop the execution of Executor ThreadPool in java?

查看:30
本文介绍了java中如何停止Executor ThreadPool的执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Java 中的 Executors,以便一次同时运行更多线程.我有一组 Runnable Objects,我将它分配给 Exceutors.The Executor 工作正常一切都很好.但是在池中执行完所有任务之后,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.

推荐答案

ExecutorService 类有 2 个方法:shutdown()shutdownNow().

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

使用shutdown()方法后,可以调用awaitTermination() 阻塞,直到所有启动的任务都完成.您甚至可以提供超时以防止永远等待.

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.

这篇关于java中如何停止Executor ThreadPool的执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆