Java Reference
In-Depth Information
ument, going back and downloading the images and drawing them into the associated place-
Downloading an image mostly involves waiting for I/O to complete, and during this time the
CPU does little work. So the sequential approach may underutilize the CPU, and also makes
the user wait longer than necessary to see the finished page. We can achieve better utilization
and responsiveness by breaking the problem into independent tasks that can execute concur-
rently.
Listing 6.10. Rendering Page Elements Sequentially.
6.3.2. Result-bearing Tasks:
Callable
and
Future
The
Executor
framework uses
Runnable
as its basic task representation.
Runnable
is a
fairly limiting abstraction;
run
cannot return a value or throw checked exceptions, although
it can have side effects such as writing to a log file or placing a result in a shared data struc-
ture.
Many tasks are effectively deferred computations—executing a database query, fetching a
resource over the network, or computing a complicated function. For these types of tasks,
Callable
is a better abstraction: it expects that the main entry point,
call
, will re-
several utility methods for wrapping other types of tasks, including
Runnable
and
java.security.PrivilegedAction
, with a
Callable
.
Runnable
and
Callable
describe abstract computational tasks. Tasks are usually finite:
they have a clear starting point and they eventually terminate. The lifecycle of a task executed