Java Reference
In-Depth Information
If a task has to be rejected because the system is overloaded, which task should be
selected as the victim, and how should the application be notified?
What actions should be taken before or after executing a task?
Execution policies are a resource management tool, and the optimal policy depends on the
available computing resources and your quality-of-service requirements. By limiting the
number of concurrent tasks, you can ensure that the application does not fail due to resource
exhaustion or suffer performance problems due to contention for scarce resources. [3] Separ-
ating the specification of execution policy from task submission makes it practical to select
an execution policy at deployment time that is matched to the available hardware.
Whenever you see code of the form:
new Thread(runnable).start()
and you think you might at some point want a more flexible execution policy, seriously con-
sider replacing it with the use of an Executor .
6.2.3. Thread Pools
A thread pool, as its name suggests, manages a homogeneous pool of worker threads. A
thread pool is tightly bound to a work queue holding tasks waiting to be executed. Worker
threads have a simple life: request the next task from the work queue, execute it, and go back
to waiting for another task.
Executing tasks in pool threads has a number of advantages over the thread-per-task ap-
proach. Reusing an existing thread instead of creating a new one amortizes thread creation
and teardown costs over multiple requests. As an added bonus, since the worker thread often
already exists at the time the request arrives, the latency associated with thread creation does
not delay task execution, thus improving responsiveness. By properly tuning the size of the
thread pool, you can have enough threads to keep the processors busy while not having so
many that your application runs out of memory or thrashes due to competition among threads
for resources.
The class library provides a flexible thread pool implementation along with some useful pre-
defined configurations. You can create a thread pool by calling one of the static factory meth-
ods in Executors :
Search WWH ::




Custom Search