The thread pool
A thread pool is a pool of threads:
- It can help us manage threads, avoid repeated creation and destruction of threads, improve response time. Save resources.
The core parameters of the thread pool
- CorePoorSize: Maximum number of core threads in the thread pool.
- MaximumPoolSize: specifies the maximum number of threads in a thread pool.
- KeepAliveTime: specifies the keepAliveTime of non-core threads in the thread pool.
- Unit: Lifetime of idle threads in the thread pool
- ThreadFactory: Used to create thread factories.
- Handler: Saturation policy time of the thread pool.
Thread pool processing flow
Four rejection strategies
- AbortPolicy (throw exception, default)
- DiscardPolicy (Discards tasks)
- DiscardOldestPolicy (Discarding the oldest task in the thread pool and putting the new task into the queue)
- CallerRunsPolicy (to be handled by the calling thread, that is, the thread that performs the excute method)
Several common thread pools
- NewFixedThreadPool (thread pool with a fixed number of threads)
The number of core threads is the same as the total number of threads. All tasks will be executed by the core thread. If the thread is full, it will be put into the buffer queue.
- newCachedThreadPool
The number of core threads is 0, and the maximum number of threads is customized. All threads will be automatically destroyed when no task is executed for a certain period of time, which is suitable for a large number of short-term small tasks.
- newSingleThreadPool
Both the core and the maximum number of threads are 1, and only one thread is used to operate.
- newScheduledThreadPool
A thread task that executes periodically.
Thread pool state changes
1, RUNNING
(1) State description: When the thread pool is in the RUNNING state, it can receive new tasks and process the added tasks.
(2) State switchover: The initial state of the thread pool is RUNNING. In other words, once created, the thread pool is RUNNING and the number of tasks in the pool is 0!
2、 SHUTDOWN
(1) State description: When the thread pool is in the SHUTDOWN state, it does not receive new tasks, but can process the added tasks.
(2) State switch: When the shutdown() interface of the thread pool is called, the thread pool is switched from RUNNING -> shutdown.
3, STOP
(1) State description: When the thread pool is in the STOP state, no new tasks will be received, no added tasks will be processed, and the ongoing tasks will be interrupted.
(2) State switch: When the thread pool shutdownNow() interface is called, the thread pool is switched from (RUNNING or SHUTDOWN) -> STOP.
4, TIDYING
(1) State description: When all tasks have been terminated, the “task number” recorded by CTL is 0, and the thread pool will become TIDYING state. The hook function terminated() is executed when the thread pool is in TIDYING state. Terminated () is empty in the ThreadPoolExecutor class and is processed if the user wants the thread pool to become TIDYING; This can be done by overloading the terminated() function.
(2) State switch: When the thread pool is in SHUTDOWN state, the blocking queue is empty and the tasks executed in the thread pool are also empty, then SHUTDOWN -> TIDYING. When the thread pool is in the STOP state and the task executed in the thread pool is empty, STOP -> TIDYING is invoked.
5, and TERMINATED
The thread pool is TERMINATED completely and becomes TERMINATED.
Terminated () : the thread pool is in TIDYING state and terminated by TIDYING -> terminated().
The state of the thread changes
1, new: create a new thread object
2. Start: Starts the thread and waits for it to run.
3, run: obtain the CPU can enter the running state. This thread has blocked, ready, dead, running states