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

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

问题描述

我正在使用java执行程序同时运行更多的线程。
我有一组Runnable对象,我将它分配给Exceutors.Executor工作正常
,每一件事情都很好。但所有的任务在池中执行后,java程序不是终止,我认为Executor需要一些时间来杀死线程。任何人帮助我减少执行所有任务后执行器所需的时间。

解决方案

ExecutorService < a>类只有两个方法: shutdown() shutdownNow()

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



您可能想点击我提供的一些链接。他们直接去文档,你可以自己读这些东西。


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.

解决方案

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.

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

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