|
|
@@ -30,6 +30,21 @@ public class Cf08_ManualComplete {
|
|
|
forced.obtrudeValue("强制更新的结果");
|
|
|
System.out.println("obtrudeValue 覆盖后: " + forced.join());
|
|
|
|
|
|
+ // isDone: 查询任务是否"已结束"(成功、失败、取消都算 done)
|
|
|
+ CompletableFuture<String> pending = new CompletableFuture<>();
|
|
|
+ System.out.println("刚创建未完成 isDone: " + pending.isDone());
|
|
|
+ pending.complete("已赋值");
|
|
|
+ System.out.println("完成后 isDone: " + pending.isDone());
|
|
|
+
|
|
|
+ // obtrudeException: 与 obtrudeValue 成对,无论状态如何强制让其以异常结束(慎用)
|
|
|
+ CompletableFuture<String> forcedErr = CompletableFuture.completedFuture("旧结果");
|
|
|
+ forcedErr.obtrudeException(new RuntimeException("强制失败"));
|
|
|
+ try {
|
|
|
+ forcedErr.join();
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("obtrudeException 后异常: " + e.getCause());
|
|
|
+ }
|
|
|
+
|
|
|
// cancel: 取消任务, isCancelled 与 isCompletedExceptionally 都会为 true
|
|
|
CompletableFuture<String> cancelling = new CompletableFuture<>();
|
|
|
System.out.println("cancel() 返回值: " + cancelling.cancel(true));
|