选项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.