Multiple Threads Java Thread Pool seven parameters detailed

Multiple Threads Java Thread Pool seven parameters detailed

     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue}
     *         or {@code threadFactory} or {@code handler} is null
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) 

1. Core PoolSize Thread Pool Core Thread Size

A minimum number of threads is maintained in the thread pool, and they will not be destroyed even if they are idle, unless allowCoreThreadTimeOut is set. The minimum number of threads here is corePoolSize.

2. Maximum PoolSize Thread Pool Maximum Threads

When a task is committed to the thread pool, it is cached first to the work queue (described later)If the work queue is full, a new thread is created, a task is taken out of the work queue and handled by the new thread, while the newly submitted task is put into the work queue. Thread pool does not create new threads indefinitely, it has a limit on the maximum number of threads, which is specified by maximunPoolSize.

3. Keep AliveTime idle thread lifetime

If a thread is idle and the current number of threads is greater than corePoolSize, the idle thread will be destroyed after the specified time, where the specified time is set by keepAliveTime

4. unit Space Thread Lifetime Units

The unit of measure for keepAliveTime

5. workQueue Work Queue

When a new task is submitted, it is first entered into this work queue and then removed from the queue when the task is scheduled. There are four work queues available in jdk:


A bounded blocking queue based on an array, sorted by FIFO. When a new task comes in, it is placed at the end of the queue and bounded arrays prevent resource depletion. When a new task comes in after the number of threads in the thread pool reaches corePoolSize, it places the task at the end of the queue and waits for it to be dispatched. If the queue is full, create a new thread, ifIf the number of threads has reached maxPoolSize, the rejection policy is executed.


Unbounded blocking queue based on chained list (its maximum capacity is Interger.MAX)Sort by FIFO. Because the queue is nearly unbounded, when the number of threads in the thread pool reaches corePoolSize, new tasks are put in the queue all the time, instead of creating new threads until maxPoolSize, the parameter maxPoolSize actually does not work when using the work queue.


A blocked queue that does not cache a task, and the producer places a task until the consumer takes it out. That is, when a new task comes in, it is not cached, but directly dispatched for execution. If no threads are available, a new thread is created, and if the number of threads reaches maxPoolSize, a rejection policy is executed.


Unbounded blocking queue with priority, which is achieved through the parameter Comparator.

6. Thread Factory Thread Factory

The factory used when creating a new thread, which can be used to set the thread name, whether it is a daemon thread, and so on

7. handler Rejection Policy

When the maximum number of tasks in the workqueue has been reached and the maximum number of threads in the thread pool has been reached, what should be done if new tasks are submitted. The rejection policy here is to solve this problem, which is provided in jdk in four rejection policies:


Under this policy, the run method of the rejected task is executed directly in the caller thread, and the task is discarded unless the thread pool has shutdown.


Under this policy, the task is discarded directly and a RejectedExecutionException exception is thrown.


Under this strategy, you simply discard the task and do nothing.


Under this policy, discard the earliest task in the queue and try to queue the rejected task

Keywords: Java Interview

Added by jabapyth on Tue, 14 Sep 2021 19:42:14 +0300