是否使用invokeAll或submit - java Executor服务 [英] Whether to use invokeAll or submit - java Executor service

查看:640
本文介绍了是否使用invokeAll或submit - java Executor服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我必须为同一个callable异步执行5个线程。据我了解,有两种选择:

I have a scenario where I have to execute 5 thread asynchronously for the same callable. As far as I understand, there are two options:

1)使用提交(可调用)

1) using submit(Callable)

ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<String>> futures = new ArrayList<>();
for(Callable callableItem: myCallableList){
    futures.add(executorService.submit(callableItem));
}

2)使用invokeAll(Callable of Collelable)

2) using invokeAll(Collections of Callable)

ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<String>> futures = executorService.invokeAll(myCallableList));




  1. 首选方式应该是什么?

  2. 与其他产品相比,它们是否存在任何劣势或性能影响?


推荐答案

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

Option 1 : You are submitting the tasks to ExecutorService and you are not waiting for the completion of all tasks, which have been submitted to ExecutorService

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

Option 2 : You are waiting for completion of all tasks, which have been submitted to ExecutorService.


首选方式应该是什么?

What should be the preferred way?

根据应用要求,首选其中任何一个。

Depending on application requirement, either of them is preferred.


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

  2. 如果您需要等待所有已提交的任务完成d到 ExecutorService ,首选选项2

  1. If you don't to wait after task submit() to ExecutorService, prefer Option 1.
  2. If you need to wait for completion of all tasks, which have been submitted to ExecutorService, prefer Option 2.




与其他产品相比,是否存在任何劣势或性能影响?

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

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

If your application demands Option 2, you have to wait for completion of all submitted tasks to ExecutorService unlike in Option 1. Performance is not criteria for comparison as both are designed for two different purposes.

还有一件事:无论你喜欢什么选项, FutureTask 吞下任务执行期间的异常。你必须要小心。看看这个SE问题:处理ThreadPoolExecutor的例外

And one more important thing : Whatever option you prefer, FutureTask swallows Exceptions during task execution. You have to be careful. Have a look at this SE question: Handling Exceptions for ThreadPoolExecutor

使用Java 8,您还有一个选项: ExecutorCompletionService

With Java 8, you have one more option : ExecutorCompletionService


CompletionService 使用提供的Executor执行任务。该类安排提交的任务在完成后放置在可使用take访问的队列中。该类很轻,足以在处理任务组时适合短暂使用。

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.

查看相关的SE问题: ExecutorCompletionService?如果我们有invokeAll,为什么需要一个?

Have a look at related SE question : ExecutorCompletionService? Why do need one if we have invokeAll?

这篇关于是否使用invokeAll或submit - java Executor服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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