Java Reference
In-Depth Information
consult the source code for Executors to see the execution policies for the default config-
urations and use them as a starting point. ThreadPoolExecutor has several constructors,
the most general of which is shown in Listing 8.2 .
8.3.1. Thread Creation and Teardown
The core pool size, maximum pool size, and keep-alive time govern thread creation and tear-
down. The core size is the target size; the implementation attempts to maintain the pool at
this size even when there are no tasks to execute, [2] and will not create more threads than this
unless the work queue is full. [3] The maximum pool size is the upper bound on how many
pool threads can be active at once. A thread that has been idle for longer than the keep-alive
time becomes a candidate for reaping and can be terminated if the current pool size exceeds
the core size.
Listing 8.2. General Constructor for ThreadPoolExecutor .
By tuning the core pool size and keep-alive times, you can encourage the pool to reclaim re-
sources used by otherwise idle threads, making them available for more useful work. (Like
everything else, this is a tradeoff: reaping idle threads incurs additional latency due to thread
creation if threads must later be created when demand increases.)
The newFixedThreadPool factory sets both the core pool size and the maximum pool
size to the requested pool size, creating the effect of infinite timeout; the
newCachedThreadPool factory sets the maximum pool size to Integer.MAX_VALUE
and the core pool size to zero with a timeout of one minute, creating the effect of an infinitely
expandable thread pool that will contract again when demand decreases. Other combinations
are possible using the explicit ThreadPool-Executor constructor.
Search WWH ::




Custom Search