There are four constructors in the ThreadPoolExecutor class, which ultimately call the following function:

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)
Copy the code

The constructor takes seven arguments as follows:

corePoolSize

The number of core threads in the thread pool that, when a task is submitted, creates a new thread to execute the task until the current number of threads equals corePoolSize; If the current number of threads is corePoolSize, further submitted tasks are stored in a blocking queue, waiting to be executed. If the thread pool’s prestartAllCoreThreads() method is executed, the thread pool creates and starts all core threads ahead of time. When the number of threads is less than or equal to corePoolSize, by default the thread will always live in the thread pool, even if the thread is idle. If allowCoreThreadTimeOut is set to true, threads that remain idle for more than a certain amount of time will be destroyed regardless of the number of threads.

maximumPoolSize

The maximum number of threads allowed in the thread pool. If the current blocking queue is full and the task continues to be submitted, a new thread is created to execute the task, provided that the current number of threads is less than maximumPoolSize;

keepAliveTime

The lifetime of a thread when it is idle, that is, the lifetime of a thread when there is no task to execute; By default, this parameter is only useful if the number of threads is greater than corePoolSize; If allowCoreThreadTimeOut is set to true, threads that remain idle for more than a certain amount of time will be destroyed regardless of the number of threads.

unit

The unit of keepAliveTime. TimeUnit is an enumeration type that includes:

  • NANOSECONDS: 1 micromillisecond = 1 microsecond / 1000
  • MICROSECONDS: 1 microsecond = 1 millisecond / 1000
  • MILLISECONDS: 1 millisecond = 1 second /1000
  • SECONDS: SECONDS
  • MINUTES: points
  • HOURS: HOURS
  • DAYS: day

workQueue

A blocking queue used to hold tasks that are waiting to be executed and that must implement the Runable interface, such as the following blocking queue:

  • ArrayBlockingQueue: a bounded blocking queue based on an array structure that sorts tasks by FIFO;
  • LinkedBlockingQuene: an unbounded blocking queue based on a linked list structure that sorts tasks by FIFO and generally has a higher throughput than ArrayBlockingQuene;
  • SynchronousQuene: a blocking queue that does not store elements. Each insert operation must wait until another thread calls a remove operation. Otherwise, the insert operation is blocked and throughput is usually higher than LinkedBlockingQuene.

threadFactory

Create a thread factory, through the custom thread factory can be set to each new thread with a recognition of the thread name, such as:

public class OneMoreThreadFactory implements ThreadFactory {
    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    public OneMoreThreadFactory(a) {
        namePrefix = "OneMoreThread-" + poolNumber.getAndIncrement() + "-";
    }

    @Override
    public Thread newThread(Runnable r) {
        return newThread( r, namePrefix + threadNumber.getAndIncrement()); }}Copy the code

handler

The saturation strategy of the thread pool. When the blocking queue is full and there are no idle worker threads, if the task continues to be submitted, a policy must be adopted to process the task. The thread pool provides four strategies:

  • AbortPolicy: Directly throws an exception, the default policy.
  • CallerRunsPolicy: Executes the task with the caller’s thread;
  • DiscardOldestPolicy: Discards the most advanced task in the blocking queue and executes the current task.
  • DiscardPolicy: Directly discards the task.

The article continues to update, wechat search “Ten thousand Cat Society” first read. After following the reply “ebook”, free access to 12 Java required reading technical books.