Unless specified otherwise, ThreadPoolExecutor
uses a default ThreadFactory
that creates all threads in the same thread group.
If fewer than corePoolSize
threads are running, a new thread is created to handle every incoming request, even if other worker threads are idle. Keep-alive does not apply to core threads. Once the core threads were created, the executor will only create additional threads (up to maxPoolSize
) only when the queue is full.
If a new task is submitted when there are maxPoolSize threads and the queue is full, that task will be rejected. The behavior of the "rejection" is defined by the RejectedExecutionHandler
. By default, the rejection handler is AbortPolicy
(throw a runtime RejectedExecutionException upon rejection). You should analyze whether it is correct to use such policy, or perhaps set another one such as CallerRunsPolicy
(i.e. run the task in the thread that has invoked submit
, instead of enqueueing).