ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
Copy the code
CorePoolSize Specifies the number of core threads
The number of core threads in the thread pool. If the thread pool receives a task and the number of threads in the thread pool does not reach corePoolSize, the thread pool directly creates a new thread for the task to handle. Create a Work object that holds a reference to the task Runnable and Thread. Finally, put the Work into a Set called Workers. 0 =< workers. Size <=maximumPoolSize.
MaximumPoolSize Specifies the maximum number of threads allowed
The number of threads in the thread pool has reached the number of core threads (corePoolSize), and the task queue is full. If more tasks are submitted, the number of threads in the thread pool reaches maximumPoolSize. If the number of threads in the thread pool is smaller than maximumPoolSize, the task queue is full. New threads will continue to be created using thread factories. If the number of threads in the thread pool is equal to maximumPoolSize, no more threads will be created and RejectedExecutionHandler will be triggered. The newly created object is also a Work object and is eventually put into the Workers collection.
KeepAliveTime and Unit exceed the keepAliveTime of the thread
When the number of threads in the thread pool is greater than corePoolSize, the extra threads are destroyed after keepAliveTime.
4. WorkQueue Indicates the task queue
A queue of tasks that a thread pool needs to execute, usually with a fixed number of ArrayBlockingQueues and an unlimited number of linkedBlockingQueues.
ThreadFactory threadFactory, used to create threads
There are initially no threads in the thread pool, but when the task arrives, the thread factory is used to create the thread.
6. Handler task rejection policy
When the task queue is full and a new task comes in, this interface is called back. There are several default implementations, and it is generally recommended to implement them yourself depending on your business.