ListenableFuture与CompletableFuture比较
Both ListenableFuture and CompletableFuture have an advantage over its parent class Future by allowing the caller to "register" in one way or another a callback to be called when the async action has been completed.
With Future you can do this:
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。ExecutorService executor = ...; Future f = executor.submit(...); f.get();
f.get()
gets blocked until the async action is completed.
With ListenableFuture you can register a callback like this:
ListenableFuture listenable = service.submit(...); Futures.addCallback(listenable, new FutureCallback<Object>() { @Override public void onSuccess(Object o) { //handle on success } @Override public void onFailure(Throwable throwable) { //handle on failure } })
With CompletableFuture you can also register a callback for when the task is complete, but it is different from ListenableFuture in that it can be completed from any thread that wants it to complete.
CompletableFuture completableFuture = new CompletableFuture(); completableFuture.whenComplete(new BiConsumer() { @Override public void accept(Object o, Object o2) { //handle complete } }); // complete the task completableFuture.complete(new Object())
When a thread calls complete on the task, the value received from a call to get() is set with the parameter value if the task is not already completed.

更多精彩