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

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

问题描述

我有一个场景,我必须为同一个可调用对象异步执行 5 个线程.据我了解,有两种选择:

1) 使用提交(Callable)

ExecutorService executorService = Executors.newFixedThreadPool(5);列表<未来<字符串>>期货 = 新的 ArrayList<>();for(Callable callableItem: myCallableList){futures.add(executorService.submit(callableItem));}

2) 使用 invokeAll(Collections of Callable)

ExecutorService executorService = Executors.newFixedThreadPool(5);列表<未来<字符串>>期货 = executorService.invokeAll(myCallableList));

  1. 什么是首选方式?
  2. 与另一个相比,它们中的任何一个是否有任何缺点或性能影响?

解决方案

Option 1 : 您正在将任务提交给 ExecutorService 而不是等待完成所有已提交给 ExecutorService

的任务

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

<块引用>

什么是首选方式?

根据应用要求,两者中的一个是首选.

  1. 如果您不想在任务 submit() 到 ExecutorService 之后等待,请选择 Option 1.
  2. 如果您需要等待所有已提交给ExecutorService的任务完成,请首选Option 2.

<块引用>

与另一个相比,它们中的任何一个有任何缺点或性能影响吗?

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

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

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

<块引用>

一个 CompletionService,它使用提供的 Executor 来执行任务.此类安排提交的任务在完成后放置在使用 take 可访问的队列中.该类足够轻量级,适合在处理任务组时临时使用.

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

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) using submit(Callable)

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

2) using invokeAll(Collections of Callable)

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

  1. What should be the preferred way?
  2. Is there any disadvantage or performance impact in any of them compared to the other one?

解决方案

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

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. If you don't want 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?

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

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

With Java 8, you have one more option: ExecutorCompletionService

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.

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

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

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