igr*_*igr 5 java future java-8 completable-future
我有一些使用的异步运行流程CompletableFuture,例如:
foo(...)
.thenAccept(aaa -> {
if (aaa == null) {
break!
}
else {
...
}
})
.thenApply(aaa -> {
...
})
.thenApply(...
Run Code Online (Sandbox Code Playgroud)
因此,如果我的foo()返回null(将来)我需要很快打破,否则,流程继续.
现在我必须null在每个未来的街区检查所有方式; 但那很难看.
这有可能CompletableFuture吗?
有了CompletableFuture你可以定义你的异步任务,被执行的一个接一个的流动.例如,您可以说:
做A,当A完成时,做B,然后做C ......
我的问题是打破这种流动并说:
做A,但如果结果为null break; 否则做B,然后做C ......
这就是我所说的"打破流动".
你的问题很笼统,所以答案可能并不完全适用于你。有许多不同的方法可以解决这个问题,这些方法可能适用于不同的情况。
在评估中创建分支的一种方法CompletableFuture是.thenCompose:
foo().thenCompose(x -> x == null
? completedFuture(null)
: completedFuture(x)
.then.....(...)
.then.....(...)
).join();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
753 次 |
| 最近记录: |