java判断符合条件时停止,Java ServiceExecutor终止条件

I'm new to java executor stuff.

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.

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?

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

解决方案

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值