等待 Java 异步调用完成

我有一个异步函数调用其他异步函数。在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。而不是等待异步完成其执行。我在验证了日志语句中添加了相同的日志语句。如果我的理解是错误的,请纠正我。建议任何其他可以帮助我的概念。


噜噜哒
浏览 186回答 2
2回答

qq_笑_17

下面是一个使用倒计时批处理的示例。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 {&nbsp; &nbsp; public static void main(String[] args) throws InterruptedException {&nbsp; &nbsp; &nbsp; &nbsp; final CountDownLatch latch = new CountDownLatch(1);&nbsp; &nbsp; &nbsp; &nbsp; final ExecutorService executorService = Executors.newCachedThreadPool();&nbsp; &nbsp; &nbsp; &nbsp; Runnable runnableA = () -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Runnable A");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; latch.countDown();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Runnable A finished");&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Runnable runnableB = () -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Runnable B");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; executorService.submit(runnableA);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Runnable B waiting for A to complete");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; latch.await();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Runnable B finished");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Thread interrupted");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.currentThread().interrupt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; executorService.submit(runnableB);&nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(10);&nbsp; &nbsp; &nbsp; &nbsp; shutDown(executorService);&nbsp; &nbsp; }&nbsp; &nbsp; private static void shutDown(ExecutorService executorService) {&nbsp; &nbsp; &nbsp; &nbsp; executorService.shutdown();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; executorService.shutdownNow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; executorService.shutdownNow();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我使用方法使主线程休眠,因为在提交任务 B 后立即关闭池可能会导致池在任务 B 提交任务 A 之前停止接受新任务。Thread.sleep()

DIEA

一种方法是使用java锁定方法。例如:private AtomicBoolean&nbsp; &nbsp;processed = new AtomicBoolean(true) ;private String&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = null ;public String doAndWait(){&nbsp; &nbsp; synchronized(processed) {&nbsp; &nbsp; &nbsp; &nbsp; doSomethingAsync() ;&nbsp; &nbsp; &nbsp; &nbsp; processed.wait();&nbsp; &nbsp; }&nbsp; &nbsp; return result ;}public void doSomethingAsync(){&nbsp; &nbsp; ...&nbsp; &nbsp; result="OK";&nbsp; &nbsp; synchronized(processed) {&nbsp; &nbsp; &nbsp; &nbsp; processed.notify();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java