, , , CompletableFuture
. , .
, CompletableFuture
, , , CompletableFuture
"", , , , .
Future
s , , , CompletableFuture
, . CompletableFuture.cancel(boolean)
:
:
mayInterruptIfRunning - , .
, future1
, future2
, , many
, , cancel
many
. , , , , future1
future2
, , , .
:
CompletableFuture<String> supply = CompletableFuture.supplyAsync(() -> {
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));
System.out.println("supplying value");
return "foo";
});
CompletableFuture<String> then = supply.thenApply(s -> {
System.out.println("Evaluating next stage");
return s;
});
CompletableFuture<?> last = then.handle((s,t) -> {
System.out.println("last stage: value: "+s+", throwable: "+t);
return "";
});
System.out.println("cancelling: "+supply.cancel(true));
ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.DAYS);
:
last stage: value: null, throwable: java.util.concurrent.CompletionException: java.util.concurrent.CancellationException
canceling: true
supplying value
( )
, supply.cancel(true)
then.cancel(true)
true
false
; Supplier
.
, , , , , CompletableFuture
. , service.request(paramN)
.
CompletableFuture
, , , complete
, , CompletableFuture
, complete
. , , cancel
, , , .
, cancel
many
, cancel
future1
future2
, .