第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

等待 Java 異步調(diào)用完成

等待 Java 異步調(diào)用完成

嚕嚕噠 2022-09-14 10:38:56
我有一個(gè)異步函數(shù)調(diào)用其他異步函數(shù)。在Java中,如何等到異步調(diào)用完成(包括其中的任何嵌套異步調(diào)用)。我已經(jīng)可以調(diào)用未來(lái)了,但沒(méi)有運(yùn)氣。示例代碼:void asyncMehodA(){ }void asyncMethodB() {asyncMehodA();}我嘗試了未來(lái)可調(diào)用的方式: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 函數(shù)將阻塞執(zhí)行,直到異步返回。但似乎 get 函數(shù)將觸發(fā)異步調(diào)用并恢復(fù) null。而不是等待異步完成其執(zhí)行。我在驗(yàn)證了日志語(yǔ)句中添加了相同的日志語(yǔ)句。如果我的理解是錯(cuò)誤的,請(qǐng)糾正我。建議任何其他可以幫助我的概念。
查看完整描述

2 回答

?
qq_笑_17

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊

下面是一個(gè)使用倒計(jì)時(shí)批處理的示例。

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();

        }


    }



}

我使用方法使主線(xiàn)程休眠,因?yàn)樵谔峤蝗蝿?wù) B 后立即關(guān)閉池可能會(huì)導(dǎo)致池在任務(wù) B 提交任務(wù) A 之前停止接受新任務(wù)。Thread.sleep()


查看完整回答
反對(duì) 回復(fù) 2022-09-14
?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊

一種方法是使用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();

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-09-14
  • 2 回答
  • 0 關(guān)注
  • 218 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)