Name

ThreadPoolExecutor

Synopsis

This class implements the ExecutorService interface to execute tasks using a highly configurable thread pool. The easiest way to instantiate this class is through the static factory methods of the Executors class. If you want a more highly configured thread pool, you can instantiate it directly.

Four configuration parameters must be passed to every ThreadPoolExecutor( ) constructor; two others are optional. Many of these parameters may also be queried and adjusted after the executor has been created through various ThreadPoolExecutor accessor methods. The most important configuration parameters specify the size of the thread pool, and the queue that the executor uses to hold tasks that it cannot currently run. corePoolSize is the number of threads that the pool should hold under normal usage. As tasks are submitted to the ThreadPoolExecutor, a new thread is created for each task until the total number of threads reaches this size.

If corePoolSize threads have already been created, newly submitted tasks are placed on the work queue. As these core threads finish the tasks they are executing, they take( ) a new task from the work queue. You must specify the workQueue when you call the ThreadPoolExecutor( ) constructor. It may be any BlockingQueue object and the behavior of the thread pool depends strongly on the behavior of the queue you specify. Options include an unbounded LinkedBlockingQueue, a bounded ArrayBlockingQueue with a capacity of your choosing, ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.