Java Reference
In-Depth Information
Given these definitions:
The optimal pool size for keeping the processors at the desired utilization is:
You can determine the number of CPUs using Runtime :
int N_CPUS = Runtime.getRuntime().availableProcessors();
Of course, CPU cycles are not the only resource you might want to manage using thread
pools. Other resources that can contribute to sizing constraints are memory, file handles,
socket handles, and database connections. Calculating pool size constraints for these types of
resources is easier: just add up how much of that resource each task requires and divide that
into the total quantity available. The result will be an upper bound on the pool size.
When tasks require a pooled resource such as database connections, thread pool size and re-
source pool size affect each other. If each task requires a connection, the effective size of the
thread pool is limited by the connection pool size. Similarly, when the only consumers of
connections are pool tasks, the effective size of the connection pool is limited by the thread
pool size.
8.3. Configuring ThreadPoolExecutor
ThreadPoolExecutor provides the base implementation for the executors returned by
the newCachedThreadPool , newFixedThreadPool , and newScheduled-
ThreadExecutor factories in Executors . ThreadPoolExecutor is a flexible, ro-
bust pool implementation that allows a variety of customizations.
If the default execution policy does not meet your needs, you can instantiate a
ThreadPoolExecutor through its constructor and customize it as you see fit; you can
Search WWH ::




Custom Search