java中的方法优化

我是 Java 新手。我正在尝试优化以下方法:


public void myLongRunningMethod()

{

LongRunningOperation1();


LongRunningOperation2();


LongRunningOperation3();


Log.Info("completion message goes here.")


}

LongRunningOperation1()、LongRunningOperation2() 和 LongRunningOperation3() 相互独立,它们的完成顺序无关紧要。


但是只有在所有这些方法调用成功完成后才应该打印日志语句。


如果我采用以下方法,由于它使用新线程,我相信方法的完成顺序不会得到保证。


public String myMethod()

{


Thread thread1 = new Thread(() -> {

    LongRunningOperation1();

}).start();



Thread thread2 = new Thread(() -> {

    LongRunningOperation2();

}).start();



Thread thread3 = new Thread(() -> {

    LongRunningOperation3();

}).start();


Log.Info("completion message goes here.")


}


慕娘9325324
浏览 105回答 2
2回答

海绵宝宝撒

正如评论所提到的,最简单的方法是在 3 个线程中的每一个上调用 join() :thread1.join(); thread2.join(); thread3.join();或者使用 aCountDownLatch虽然它涉及更多(但在概念上可能更“正确”):latch = new CountDownLatch(3)在完成任务latch.countDown() 后让每个线程去做。让主线程latch.await()- 瞧。

慕工程0101907

与其手动创建Threads ,不如使用ExecutorService API. 使用ExecutorService您可以提交长时间运行的操作,结果您将收到Future实例,让您等到操作完成。请参阅下面的示例,该示例显示了该想法:import java.util.ArrayList;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;public class Threads {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new Threads().myMethod();&nbsp; &nbsp; }&nbsp; &nbsp; private String myMethod() {&nbsp; &nbsp; &nbsp; &nbsp; ExecutorService executor = Executors.newFixedThreadPool(3);&nbsp; &nbsp; &nbsp; &nbsp; List<Future<?>> futures = new ArrayList<>(3);&nbsp; &nbsp; &nbsp; &nbsp; futures.add(executor.submit(this::LongRunningOperation1));&nbsp; &nbsp; &nbsp; &nbsp; futures.add(executor.submit(this::LongRunningOperation2));&nbsp; &nbsp; &nbsp; &nbsp; futures.add(executor.submit(this::LongRunningOperation3));&nbsp; &nbsp; &nbsp; &nbsp; for (Future<?> future : futures) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; future.get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("All operations are finished!");&nbsp; &nbsp; &nbsp; &nbsp; return "Done";&nbsp; &nbsp; }&nbsp; &nbsp; private void LongRunningOperation1() {&nbsp; &nbsp; &nbsp; &nbsp; sleep(TimeUnit.SECONDS.toMillis(1));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("LongRunningOperation1 is finished!");&nbsp; &nbsp; }&nbsp; &nbsp; private void LongRunningOperation2() {&nbsp; &nbsp; &nbsp; &nbsp; sleep(TimeUnit.SECONDS.toMillis(2));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("LongRunningOperation2 is finished!");&nbsp; &nbsp; }&nbsp; &nbsp; private void LongRunningOperation3() {&nbsp; &nbsp; &nbsp; &nbsp; sleep(TimeUnit.SECONDS.toMillis(3));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("LongRunningOperation3 is finished!");&nbsp; &nbsp; }&nbsp; &nbsp; private void sleep(long millis) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(millis);&nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}上面的代码打印:LongRunningOperation1 is finished!LongRunningOperation2 is finished!LongRunningOperation3 is finished!All operations are finished!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java