Most commonly, {@code ListenableFuture} is used as an input to anotherderived {@code Future}, as in {@link Futures#allAsList(Iterable) Futures.allAsList}. Many such methods are impossible to implement efficiently without listener support.
It is possible to call {@link #addListener addListener} directly, but thisis uncommon because the {@code Runnable} interface does not provide directaccess to the {@code Future} result. (Users who want such access may prefer{@link Futures#addCallback Futures.addCallback}.) Still, direct {@code addListener} calls are occasionally useful:
{@code final String name = ...; inFlight.add(name); ListenableFuturefuture = service.query(name);}future.addListener(new Runnable() public void run() { processedCount.incrementAndGet(); inFlight.remove(name); lastProcessed.set(name); logger.info("Done with {0}", name); } }, executor);}
Developers are encouraged to return {@code ListenableFuture} from theirmethods so that users can take advantages of the utilities built atop the class. The way that they will create {@code ListenableFuture} instancesdepends on how they currently create {@code Future} instances:
Occasionally, an API will return a plain {@code Future} and it will beimpossible to change the return type. For this case, we provide a more expensive workaround in {@code JdkFutureAdapters}. However, when possible, it is more efficient and reliable to create a {@code ListenableFuture} directly. @author Sven Mawson @author Nishant Thakkar @since 1.0
|
|
|
|