Threadpoolexecutor Introduction
Threadpoolexecutor is a thread pool class.
In layman's words, it is a collection of threads that hold a certain number of threads. The thread pool allows multiple threads to run concurrently, the number of threads running concurrently is the capacity of the thread pool, and some threads are blocked, and the thread pool is also handled by a response rejection policy at this time.
Constructor parameter description
- corepoolsize: Number of threads saved in the pool, including idle threads
- maximumpoolsize: Maximum number of threads allowed in the pool
- KeepAliveTime: When the number of threads is greater than the core, this is the maximum time to wait for a new task before terminating the extra idle thread
- Unit: Time unit of the KeepAliveTime parameter
- workQueue: The queue that was used to hold the task before execution (a task that was committed but not yet executed). This queue only saves runable tasks submitted by the Execute method
- threadfactory: The factory used when the execution program creates a new thread
- handler: Deny policy, the handler that is used when execution is blocked because it exceeds the thread range and the queue capacity
The relationship between Corepoolsize and Maximumpoolsize
If the number of running threads is less than corepoolsize, Executor always prefers to add new threads without queuing;
If the thread running is greater than or equal to corepoolsize, then Executor always prefers to join the request to the blocking queue without adding a new thread;
If the request cannot be joined to the queue, a new thread is created, and if this thread is created beyond maximumpoolsize, the task is rejected in this case;
If you set the same corepoolsize and Maximumpoolsize, a fixed-size thread pool is created. If you set Maximumpoolsize to a basic unbounded value (Integer.max_value), the pool is allowed to adapt to any number of concurrent tasks until the memory runs out. In most cases, the core pool size and the maximum pool size are set when the thread pool is created, but can also be changed dynamically through Setcorepoolsize () and Setmaximumpoolsize ().
Data structure of Threadpoolexecutor
- workers: Workers is a hashset<worker> type. A worker corresponds to a thread, and the thread pool contains multiple threads through workers. When the thread pool is started, it executes the task in the thread pooling, and after execution, it takes the blocking task out of the blocking queue of the thread-pool to execute. The thread pool implements "allow multithreading to run simultaneously" through it
- WorkQueue: WorkQueue block queue type for blockingqueue<runnable>. When the number of threads in the thread pool exceeds its capacity, the thread enters the blocking queue for blocking waits. The thread pool implements the threading blocking function through it
- mainlock: Condition type, through which mutually exclusive access to the thread pool is implemented
- corepoolsize: Core Pool Size
- maximumpoolsize: Max pool Size
- poolsize: The actual size of the current thread pool, that is, the number of tasks in the thread pools
The thread pool manages multi-threading through the Workers collection, which executes the corresponding task when it is started. When the task finishes executing, it takes the task out of the blocking queue and resumes execution. The blocking queue is responsible for managing tasks that the thread pool is too late to handle, and when the add thread pool is added to the maximum thread pool size, a deny policy (handler) is executed to handle the currently committed but rejected task.
Java Multithreading Series--The threadpoolexecutor of thread pool principle