亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java中的方法優化

java中的方法優化

慕娘9325324 2022-07-06 16:00:50
我是 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.")}
查看完整描述

2 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

正如評論所提到的,最簡單的方法是在 3 個線程中的每一個上調用 join() :thread1.join(); thread2.join(); thread3.join();

或者使用 aCountDownLatch雖然它涉及更多(但在概念上可能更“正確”):

  • latch = new CountDownLatch(3)

  • 在完成任務latch.countDown() 讓每個線程去做。

  • 讓主線程latch.await()- 瞧。


查看完整回答
反對 回復 2022-07-06
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

與其手動創建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 {


    public static void main(String[] args) {

        new Threads().myMethod();

    }


    private String myMethod() {

        ExecutorService executor = Executors.newFixedThreadPool(3);

        List<Future<?>> futures = new ArrayList<>(3);

        futures.add(executor.submit(this::LongRunningOperation1));

        futures.add(executor.submit(this::LongRunningOperation2));

        futures.add(executor.submit(this::LongRunningOperation3));

        for (Future<?> future : futures) {

            try {

                future.get();

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        System.out.println("All operations are finished!");

        return "Done";

    }


    private void LongRunningOperation1() {

        sleep(TimeUnit.SECONDS.toMillis(1));

        System.out.println("LongRunningOperation1 is finished!");

    }


    private void LongRunningOperation2() {

        sleep(TimeUnit.SECONDS.toMillis(2));

        System.out.println("LongRunningOperation2 is finished!");

    }


    private void LongRunningOperation3() {

        sleep(TimeUnit.SECONDS.toMillis(3));

        System.out.println("LongRunningOperation3 is finished!");

    }


    private void sleep(long millis) {

        try {

            Thread.sleep(millis);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

}

上面的代碼打?。?/p>


LongRunningOperation1 is finished!

LongRunningOperation2 is finished!

LongRunningOperation3 is finished!

All operations are finished!


查看完整回答
反對 回復 2022-07-06
  • 2 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號