简体   繁体   English

在 Java 中执行 2 个独立任务的正确方法

[英]Right way to execute 2 independent tasks in Java

I have 2 independent tasks to execute.我有 2 个独立的任务要执行。 I need the result of Task1 first followed by task2.我首先需要Task1的结果,然后是task2。

Which of the below 2 ways is better in terms of number of threads and performance(if at all) and why?就线程数和性能(如果有的话)而言,以下两种方式中哪一种更好,为什么? Will there be any performance difference between the 2 methods?两种方法之间会有任何性能差异吗?

Method1:方法1:

1. Execute Task1 & Task2 asynchronously by submitting them to ExecutorService
2. Get the results by calling future1.get() followed future2.get()

Method2:方法2:

1. Execute task2 asynchronously by submitting it to ExecutorService.
2. Execute Task1 on main thread
3. Get the result of task2 by calling future2.get()

The first is better from human point of view:从人类的角度来看,第一个更好:

  • Task 1 and Task 2 are equivalent tasks任务 1 和任务 2 是等效的任务
  • Both are executed as secondary tasks两者都作为次要任务执行
  • You explicitly define the order of threads to wait您明确定义要等待的线程的顺序

The second is better from jvm point of view:从 jvm 的角度来看,第二个更好:

  • There is just one external thread added to the main thread (instead of 2)主线程中只添加了一个外线程(而不是 2 个)
  • You wait only for the second result你只等待第二个结果

But generally over optimize the code is not a good idea if the resulting code is less human readable.但是,如果生成的代码可读性较差,通常过度优化代码并不是一个好主意。 My tip is:我的建议是:

Don't optimize the code if not needed.如果不需要,不要优化代码。

I would think method 1 is preferable.我认为方法1更可取。 You are kicking off both tasks, then telling the compiler that you have nothing else to do until both tasks complete.您正在启动这两个任务,然后告诉编译器在这两个任务完成之前您无事可做。 Once both tasks are complete you can proceed.两项任务都完成后,您可以继续。 You leave any decisions/optimisations on how to run the tasks to the JVM which is best positioned to make those decisions.您将有关如何运行任务的任何决定/优化留给最适合做出这些决定的 JVM。

I would suspect that there is little difference with respect to threads, processing time, etc. to either approach once the code is compiled and optimised by the JVM - the 'work' to be done is the same either way.我怀疑一旦代码由 JVM 编译和优化,两种方法在线程、处理时间等方面几乎没有区别——无论哪种方式,要完成的“工作”都是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在java中执行命令的正确方法是什么 - What is the right way to execute a command in java 推荐使用RXJava执行异步任务的方法 - Recommended way to execute async tasks with RXJava 带有Hazelcast的Java ScheduledExecutorService在Weblogic集群中执行任务 - Java ScheduledExecutorService with Hazelcast to execute tasks in weblogic cluster Java 7:如何批量执行并行任务? - Java 7: How to execute parallel tasks in batches? Java中检测处理器数量的平台无关方式 - Platform Independent Way of Detecting the Number of Processors in Java 使用 parallelStream 进行独立任务? - using parallelStream for independent tasks? Joda-获得独立日光节约时间的正确方法 - Joda - the right way to get daylight savings independent times 什么时候应该使用多线程? 如果不同的线程执行相互独立的任务,多线程是否有益? - When should you use multithreading? And would multi threading be beneficial if the different threads execute mutually independent tasks? 如何在Android设备上执行独立于平台的Java代码? - How can I execute a platform independent Java code on an Android device? 为什么java线程在独立代码之后执行? - Why does java Thread execute after the independent code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM