为什么 CompletableFuture runAsync 在我的代码中不起作用?

问题描述 投票:0回答:1

我有示例代码,例如

private Boolean exampleFunc(Long id) {
  Info info= new Info();
  info.setCustomerId(100L);
  var res = CompletableFuture.runAsync(() -> exampleApi.doFunc(info));
  return TRUE;
}

在此代码中,“res”给出

java.util.concurrent.CompletableFuture@22da05fc[未完成]

当我添加

ForkJoinPool.commonPool().awaitQuiescence(10000, TimeUnit.SECONDS);

“res”给出

java.util.concurrent.CompletionException:java.lang.NullPointerException

我无法调试 doFunc(),它不进入方法。

我该怎么办?

java asynchronous java-11 completable-future
1个回答
0
投票

看起来您的

Runnable
操作已生成
NullPointerException
。您可以使用
res.handle()
将错误处理回调添加到操作中,这只是打印出详细信息:

res.handle((r, ex) -> {
    System.out.println("handle() result="+r+" exc="+ex);
    if (ex != null)
        ex.printStackTrace();
    return r; 
});
© www.soinside.com 2019 - 2024. All rights reserved.