Java Reference
In-Depth Information
Chapter 6. Task Execution
Most concurrent applications are organized around the execution of tasks : abstract, discrete
units of work. Dividing the work of an application into tasks simplifies program organization,
facilitates error recovery by providing natural transaction boundaries, and promotes concur-
rency by providing a natural structure for parallelizing work.
6.1. Executing Tasks in Threads
The first step in organizing a program around task execution is identifying sensible taskbound-
aries . Ideally, tasks are independent activities: work that doesn't depend on the state, result,
or side effects of other tasks. Independence facilitates concurrency, as independent tasks can
be executed in parallel if there are adequate processing resources. For greater flexibility in
scheduling and load balancing tasks, each task should also represent a small fraction of your
application's processing capacity.
Server applications should exhibit both good throughput and good responsiveness under nor-
mal load. Application providers want applications to support as many users as possible, so as
to reduce provisioning costs per user; users want to get their response quickly. Further, ap-
plications should exhibit graceful degradation as they become overloaded, rather than simply
falling over under heavy load. Choosing good task boundaries, coupled with a sensible task
execution policy (see Section 6.2.2 ), can help achieve these goals.
Most server applications offer a natural choice of task boundary: individual client requests.
Web servers, mail servers, file servers, EJB containers, and database servers all accept requests
via network connections from remote clients. Using individual requests as task boundaries
usually offers both independence and appropriate task sizing. For example, the result of sub-
mitting a message to a mail server is not affected by the other messages being processed at
the same time, and handling a single message usually requires a very small percentage of the
server's total capacity.
6.1.1. Executing Tasks Sequentially
There are a number of possible policies for scheduling tasks within an application, some of
which exploit the potential for concurrency better than others. The simplest is to execute tasks
sequentially in a single thread. SingleThreadWeb-Server in Listing 6.1 processes its
tasks—HTTP requests arriving on port 80—sequentially. The details of the request process-
Search WWH ::




Custom Search