Benefits of the thread pool
1. Reduce the consumption of resources
Reduce the consumption caused by thread creation and destruction by reusing created threads
2. Improve response speed
When a task arrives, the task can be executed immediately without waiting for the thread to be created
3. Increase the manageable type of thread
Threads are scarce resources that, if created indefinitely, not only consume system resources, but also reduce system stability, using a thread pool for uniform allocation, tuning, and monitoring.
Implementation principle
When a new task is submitted to the thread pool, the thread pool process is:
1). The thread pool determines whether threads in the core line constructor are performing tasks.
If not, a new worker thread is created to perform the task. If the thread constructor the core thread is performing the task, it goes to the next process.
2). The thread pool determines whether the work queue is full.
If the work queue is not full, the newly submitted task is stored in this task column. If the work queue is full, go to the next process.
3). The thread pool determines whether threads in the thread pool are in a working state.
If not, a new worker thread is created to perform the task. If it is full, then give the saturation policy to handle the task.
Threadpoolexecutor executes the Execute () method:
Java thread pool