CompletableFuture详解~cancel
生活随笔
收集整理的这篇文章主要介绍了
CompletableFuture详解~cancel
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
取消计算
和完成异常类似,我们可以调用cancel(boolean mayInterruptIfRunning)取消计算。对于CompletableFuture类,布尔参数并没有被使用,这是因为它并没有使用中断去取消操作,相反,cancel等价于completeExceptionally(new CancellationException())。
static void cancelExample() {CompletableFuture cf = CompletableFuture.completedFuture("message").thenApplyAsync(String::toUpperCase,CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS));CompletableFuture cf2 = cf.exceptionally(throwable -> "canceled message");assertTrue("Was not canceled", cf.cancel(true));assertTrue("Was not completed exceptionally", cf.isCompletedExceptionally());assertEquals("canceled message", cf2.join()); }总结
以上是生活随笔为你收集整理的CompletableFuture详解~cancel的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Windows 安装 MongoDB 和
- 下一篇: Office 安装