[2020-03-24] Java interview questions thread pool seven arguments 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) 

A, corePoolSize core thread pool thread size

Thread pool maintains a minimum number of threads, even if the thread processing idle, they will not be destroyed unless a allowCoreThreadTimeOut. The minimum number of threads here that is corePoolSize.

 

Two, maximumPoolSize thread pool maximum number of threads

After a task is submitted to the thread pool will first cache to work queue (it will be introduced later), if the work queue is full, a new thread is created, and then handed over to the new thread taken from a work queue task to handle , but the task has just submitted into the work queue. Thread pool will not be open-ended to create a new thread, it will have a maximum limit to the number of threads, the number that is specified by the maximunPoolSize.

Three, keepAliveTime idle thread survival time

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

 

Four, unit space thread survival time unit

keepAliveTime units of measurement

 

Five, workQueue work queue

After the new task is submitted, it will first work to enter into this queue, and then remove the job from the queue when scheduling. jdk provides four work queue:

①ArrayBlockingQueue

Array-based bounded blocking queue, sorted by FIFO. After the new tasks come in, will be placed in the tail of the queue, bounded arrays can prevent resource depletion problem. When the number of threads in the thread pool reaches corePoolSize, and then there are new tasks come in, it will be the task into the tail of the queue, waiting to be scheduled. If the queue is already full, then create a new thread if the number of threads have reached maxPoolSize, it will refuse to execute strategy.

②LinkedBlockingQuene

Based on the list of unbounded blocking queue (in fact, a maximum capacity of Interger.MAX), in FIFO order. Since the approximate unboundedness, when the number of threads in the thread pool reaches corePoolSize, and then there are new tasks come in, it would have been stored in the queue, and not to create a new thread until maxPoolSize, so use the work queue for the queue, the parameters maxPoolSize in fact, it does not work.

③SynchronousQuene

A task not cache blocking queue, the producers put a task must wait until the consumer out this task. That is the new task comes in, will not be cached, but directly is scheduled to perform this task, if there is no available threads, create new threads, if the number of threads reaches maxPoolSize, refused to execute strategy.

④PriorityBlockingQueue

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

 

Six, threadFactory thread factory

The factory to use when creating a new thread, the thread can be used to set the name, whether as a daemon threads, etc.

 

Seven, handler deny policy

When the work of the task queue has reached the maximum limit, and the number of threads in the thread pool has reached the maximum limit, then if there is a new job submission come in, how to handle it. Here deny policy, it is to solve this problem, jdk provided 4 denial strategy:

①CallerRunsPolicy

Under this policy, the implementation of the run method is rejected task directly in the calling thread unless the thread pool has been shutdown, directly abandon the task.

②AbortPolicy

Under this policy, it discards the task and throw RejectedExecutionException exception.

③DiscardPolicy

Under this policy, it discards the task, doing nothing.

④DiscardOldestPolicy

Under this policy, abandoned the queue to enter the first task, then try to reject this task in the queue

Guess you like

Origin www.cnblogs.com/july-sunny/p/12560533.html