Java Reference
In-Depth Information
Figure 11.2. Concurrency vs. parallelism
11.1. Futures
The Future interface was introduced in Java 5 to model a result made available at some point in
the future. It models an asynchronous computation and provides a reference to its result that
will be available when the computation itself is completed. Triggering a potentially
time-consuming action inside a Future allows the caller Thread to continue doing useful work
instead of just waiting for the operation's result. You can think of it as taking a bag of clothes to
your favorite dry cleaner. They will give you a receipt to tell you when your clothes are cleaned (a
Future). In the meantime, you can do some other activities. Another advantage of Future is that
it's friendlier to work with than lower-level Threads. To work with a Future, you typically have to
wrap the time-consuming operation inside a Callable object and submit it to an
Executor-Service. The following listing shows an example written before Java 8.
Listing 11.1. Executing a long-lasting operation asynchronously in a
Future
 
Search WWH ::




Custom Search