Java Reference
In-Depth Information
Multiple ExecutorCompletionService s can share a single Executor , so it is per-
fectly sensible to create an ExecutorCompletionService that is private to a particular
computation while sharing a common Executor . When used in this way, a Comple-
tionService acts as a handle for a batch of computations in much the same way that a
Future acts as a handle for a single computation. By remembering how many tasks were
submitted to the CompletionService and counting how many completed results are re-
trieved, you can know when all the results for a given batch have been retrieved, even if you
use a shared Executor .
6.3.7. Placing Time Limits on Tasks
Sometimes, if an activity does not complete within a certain amount of time, the result is
no longer needed and the activity can be abandoned. For example, a web application may
fetch its advertisements from an external ad server, but if the ad is not available within two
seconds, it instead displays a default advertisement so that ad unavailability does not under-
mine the site's responsiveness requirements. Similarly, a portal site may fetch data in parallel
from multiple data sources, but may be willing to wait only a certain amount of time for data
to be available before rendering the page without it.
The primary challenge in executing tasks within a time budget is making sure that you don't
wait longer than the time budget to get an answer or find out that one is not forthcoming. The
timed version of Future.get supports this requirement: it returns as soon as the result is
ready, but throws TimeoutException if the result is not ready within the timeout period.
A secondary problem when using timed tasks is to stop them when they run out of time, so
they do not waste computing resources by continuing to compute a result that will not be
used. This can be accomplished by having the task strictly manage its own time budget and
abort if it runs out of time, or by cancelling the task if the timeout expires. Again, Future
can help; if a timed get completes with a TimeoutException , you can cancel the task
through the Future . If the task is written to be cancellable (see Chapter 7 ) , it can be termin-
ated early so as not to consume excessive resources. This technique is used in Listings 6.13
and 6.16 .
Listing 6.16 shows a typical application of a timed Future.get . It generates a composite
web page that contains the requested content plus an advertisement fetched from an ad server.
It submits the ad-fetching task to an executor, computes the rest of the page content, and then
waits for the ad until its time budget runs out. [8]
If the get times out, it cancels [9]
the ad-
fetching task and uses a default advertisement instead.
Search WWH ::




Custom Search