Thread pool
ThreadPoolExecutor
1, Constructor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, lockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }
The first three constructors are initialized by calling the fourth constructor.
Parameters:
- corePoolSize:
The size of the core thread pool. After creating a thread pool, by default, the number of threads in the thread pool is 0. When a task comes, a thread will be created to execute the task. Calling the prestartAllCoreThreads() method can preset the number of corePoolSize threads, and calling the prestartCoreThread() method can preset one thread. - maximumPoolSize:
The maximum number of threads in the thread pool indicates the maximum number of threads that can be created in the thread pool. - keepAliveTime:
The lifetime of a thread without task execution. By default, keepAliveTime takes effect when the number of threads is greater than corePoolSize. If the allowCoreThreadTimeOut(boolean value) method is called, when the number of threads is not greater than corePoolSize, the keepAliveTime parameter will also work until the number of threads is 0. - unit:
Time unit of keepAliveTime:
(1)TimeUnit.DAYS: days
(2)TimeUnit.HOURS: hours
(3)TimeUnit.MINUTES: minutes
(4)TimeUnit.SECONDS: seconds
(5)TimeUnit.MILLISECONDS: milliseconds
(6)TimeUnit.MICROSECONDS: subtle
(7)TimeUnit.NANOSECONDS: nanoseconds
- workQueue:
Blocking queue is used to store tasks waiting to be executed. The type of workQueue is BlockingQueue. Generally, the following three types can be selected:
(1) ArrayBlockingQueue: array based first in first out queue. The size of this queue must be specified when it is created;
(2) LinkedBlockingQueue: a linked list based first in first out queue. If the queue size is not specified when it is created, it defaults to integer MAX_ VALUE;
(3) SynchronousQueue: this queue is special. It will not save the submitted tasks, but will directly create a new thread to execute the new tasks. - threadFactory:
Thread factory is mainly used to create threads. - handler:
Indicates the policy when processing a task is rejected:
(1)ThreadPoolExecutor.AbortPolicy: discards the task and throws RejectedExecutionException.
(2)ThreadPoolExecutor.DiscardPolicy: also discards tasks without throwing exceptions.
(3)ThreadPoolExecutor.DiscardOldestPolicy: discard the task at the top of the queue and try to execute the task again (repeat this process)
(4)ThreadPoolExecutor.CallerRunsPolicy: this task is handled by the calling thread
2, Main methods
- execute():
Submit a task to the thread pool for execution. - submit():
A task is submitted to the thread pool and executed by the thread pool, which can return the result of task execution. Or call execute() to submit the task and use Future to obtain the task execution result. - shutdown()
Close the thread pool. At this time, the thread pool will no longer accept new tasks. It will wait for all tasks to be executed. - shutdownNow()
Close the thread pool. At this time, the thread pool will no longer accept new tasks and will try to terminate the executing tasks.
3, Thread pool status
// Indicates the status of the current thread pool. It is a volatile variable used to ensure the visibility between threads volatile int runState; // After initializing the thread pool, the thread pool is in the RUNNING state static final int RUNNING = 0; // When the shutdown() method is called, the thread pool is in the SHUTDOWN state static final int SHUTDOWN = 1; // When the shutdown now() method is called, the thread pool is in the STOP state static final int STOP = 2; // When the thread pool is in SHUTDOWN or STOP state, all working threads have been destroyed, the task cache queue has been emptied or the execution is completed, the thread pool is set to TERMINATED state static final int TERMINATED = 3;