invokeall java_是否使用invokeAll或submit – java Executor服务

选项1:您正在向ExecutorService提交任务,而您不是在等待已提交给ExecutorService的所有任务的完成

选项2:您正在等待已提交给ExecutorService的所有任务的完成.

What should be the preferred way?

根据应用要求,它们中的任何一个都是首选.

>如果您在任务submit()之后不等待ExecutorService,则更喜欢选项1.

>如果您需要等待已提交给ExecutorService的所有任务的完成,则更喜欢选项2.

Is there any disadvantage or performance impact in any of them compared to the other one?

如果您的应用程序需要选项2,则必须等待所有提交的任务完成到ExecutorService,这与选项1不同.性能不是比较的标准,因为它们都是为两个不同的目的而设计的.

还有一件更重要的事情:无论您喜欢什么选项,FutureTask都会在任务执行期间吞下Exceptions.你必须要小心.看看这个SE问题:Handling Exceptions for ThreadPoolExecutor

A CompletionService that uses a supplied Executor to execute tasks. This class arranges that submitted tasks are, upon completion, placed on a queue accessible using take. The class is lightweight enough to be suitable for transient use when processing groups of tasks.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值