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

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

等待 Java 異步調用完成

等待 Java 異步調用完成

嚕嚕噠 2022-09-14 10:38:56
我有一個異步函數調用其他異步函數。在Java中,如何等到異步調用完成(包括其中的任何嵌套異步調用)。我已經可以調用未來了,但沒有運氣。示例代碼:void asyncMehodA(){ }void asyncMethodB() {asyncMehodA();}我嘗試了未來可調用的方式:final Callable<Void> callable1 = new Callable<Void>() {            @Override            public Void call() {                asyncMethodB();                return null;            }        };final Future<Void> callableFuture = mExecutor.submit(callable1);    try {            callableFuture.get();        } catch (final InterruptedException | ExecutionException e) {}希望 get 函數將阻塞執行,直到異步返回。但似乎 get 函數將觸發異步調用并恢復 null。而不是等待異步完成其執行。我在驗證了日志語句中添加了相同的日志語句。如果我的理解是錯誤的,請糾正我。建議任何其他可以幫助我的概念。
查看完整描述

2 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

下面是一個使用倒計時批處理的示例。

package chapter13;


import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;


public class BST {


    public static void main(String[] args) throws InterruptedException {


        final CountDownLatch latch = new CountDownLatch(1);

        final ExecutorService executorService = Executors.newCachedThreadPool();


        Runnable runnableA = () -> {

            System.out.println("Runnable A");

            latch.countDown();

            System.out.println("Runnable A finished");

        };


        Runnable runnableB = () -> {

            System.out.println("Runnable B");

            executorService.submit(runnableA);

            try {

                System.out.println("Runnable B waiting for A to complete");

                latch.await();

                System.out.println("Runnable B finished");

            } catch (InterruptedException e) {

                System.out.println("Thread interrupted");

                Thread.currentThread().interrupt();

            }

        };


        executorService.submit(runnableB);


        Thread.sleep(10);


        shutDown(executorService);

    }


    private static void shutDown(ExecutorService executorService) {


        executorService.shutdown();


        try {

            if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) {

                executorService.shutdownNow();

            }

        } catch (InterruptedException e) {

            executorService.shutdownNow();

        }


    }



}

我使用方法使主線程休眠,因為在提交任務 B 后立即關閉池可能會導致池在任務 B 提交任務 A 之前停止接受新任務。Thread.sleep()


查看完整回答
反對 回復 2022-09-14
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

一種方法是使用java鎖定方法。例如:


private AtomicBoolean   processed = new AtomicBoolean(true) ;

private String          result = null ;


public String doAndWait()

{

    synchronized(processed) {

        doSomethingAsync() ;

        processed.wait();

    }

    return result ;

}


public void doSomethingAsync()

{

    ...

    result="OK";

    synchronized(processed) {

        processed.notify();

    }

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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