当前位置:   article > 正文

CompletableFuture详解~cancel_completablefuture cancel

completablefuture cancel

取消计算

和完成异常类似,我们可以调用cancel(boolean mayInterruptIfRunning)取消计算。对于CompletableFuture类,布尔参数并没有被使用,这是因为它并没有使用中断去取消操作,相反,cancel等价于completeExceptionally(new CancellationException())

  1. static void cancelExample() {
  2. CompletableFuture cf = CompletableFuture.completedFuture("message").thenApplyAsync(String::toUpperCase,
  3. CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS));
  4. CompletableFuture cf2 = cf.exceptionally(throwable -> "canceled message");
  5. assertTrue("Was not canceled", cf.cancel(true));
  6. assertTrue("Was not completed exceptionally", cf.isCompletedExceptionally());
  7. assertEquals("canceled message", cf2.join());
  8. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/824227
推荐阅读
相关标签
  

闽ICP备14008679号