Java ServiceExecutor终止条件 [英] Java ServiceExecutor terminating condition

查看:193
本文介绍了Java ServiceExecutor终止条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java executor的新手。

I'm new to java executor stuff.

我正在使用Java的ExecutorService来启动多个线程来处理数据。

I'm using Java's ExecutorService to launch several threads to process data.

Executor executor = Executors.newFixedThreadPool(poolSize);

for(int i=0; i< 5;i++) executor.execute(new MyRunnable(i));

一旦线程找不到数据,它们就会优雅地终止。

once the threads don't find data, they gracefully terminate.

我的问题是,当所有线程终止时,Executor会发生什么,它是否仍在运行其主线程?或者它将自行终止并且整个应用程序将优雅地完成?

My question is what happens to the Executor when all the threads terminate, is it still running its master thread ? or it will terminate itself and whole application will finish gracefully?

如果执行程序线程仍然运行,我怎么能让它在所有子线程完成后终止(poolSize number线程)。

in case executor thread still runs, how can I let it terminate once all its child threads are done (poolSize number of threads).

推荐答案

执行者将继续在谎言下运行线程池。您仍然可以执行或提交新任务。建议关闭Executor服务调用 ExecutorService.html #shutdown(),它会尝试停止所有正在执行的任务,停止等待任务的处理,并返回一个列表正在等待执行的任务。

Executor will keep running with it under lying thread pool. you can still execute or submit a new task. It is recommended to shutdown Executor service call ExecutorService.html#shutdown() which attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

您还可以使用 ExecutorService.html#shutdownNow()停止所有正在执行的操作任务,停止等待任务的处理,并返回等待执行的任务列表。当你需要立即关机时它很有用。

You can also use ExecutorService.html#shutdownNow() which stops all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. It is useful when you need immediate shutdown.

这篇关于Java ServiceExecutor终止条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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