The principle and use of java thread pool

In Java concurrent programming, thread pool is a concurrency framework that uses a lot of resources. By using thread pool reasonably, resource consumption can be reduced, response speed can be improved, and thread manageability can be improved.

So what is the realization principle of thread pool?

We illustrate the principle of the thread pool through the processing flow of a new task. When a new task is submitted to the thread pool, the thread pool determines whether the threads in the corePool are all performing tasks, and if not, it creates a new thread. To perform this task, otherwise, it will determine whether the BlockingQueue is full, if the BlockingQueue is not full, store the task in it, if the BlockingQueue is full, the thread pool will determine whether the threads in the thread pool are all in the working state, if If not, a new thread will be created to perform the task, otherwise the task will be handed over to the saturation strategy.

As shown in the figure above, it is the general processing flow of the thread pool.

Explain here what corePoolSize, BlockingQueue, and maximumPoolSize mean.

corePoolSize: The size of the core thread pool, that is, the number of threads in the core thread pool. If the number of threads in the core thread pool is less than this value, threads are created to process tasks.

BlockingQueue: It is a queue that is used to store tasks. When the core thread pool is full and the queue is not full, tasks are stored in the queue.

maximumPoolSize: The maximum number of threads in the thread pool. If the queue is full, but the number of thread pools has not reached this value, a thread is created for processing.

So what are the specific execution steps of the thread pool?

There are four situations in which the execute() method of the thread pool executes tasks:

1. If the number of threads currently running is less than corePoolSize, create a new thread to perform the task.

2. If the number of running threads is greater than corePoolSize, add this task to the BlockingQueue.

3. If the BlockingQueue is full, the task cannot be added to the queue, then a new thread is created to perform the task.

4. If the number of threads currently running is equal to the maximumPoolSize, the thread pool will reject the task through a policy.

The execution diagram of the thread pool is as follows:


Understand the execution process and principle of the thread pool, and then look at the use of the thread pool, mainly the setting of several parameters of the thread pool.

ThreadPoolExecutor threadPoolExecutor = new 
				ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);

We can see that the parameters that can be set are corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler. CorePoolSize and maximumPoolSize have already been explained. Let's explain what the other parameters mean.

keepAliveTime: The survival time of the thread. When the thread has no task execution, the time to keep alive. When the idle time of a thread exceeds this value, the thread will be destroyed.

unit: the time unit of keepAliveTime, optional units are days, hours, seconds, etc.

workQueue: It determines the queuing strategy of cache tasks. Different blocking queues are used for different scenarios. There are two commonly used blocking queues, SynchronousQueue and LinkedBlockingQueue.

threadFactory: Used to set the factory for creating threads. You can set a meaningful name for each created thread through the thread factory.

handler: The processing strategy of the thread pool. When the threads of the thread pool reach the maximumPoolSize, the thread pool will no longer create threads to process tasks, and the task needs to be processed through the processing strategy of the thread pool.

This is the end of the thread pool. Anyone who feels useful will like it.

Guess you like

Origin blog.csdn.net/wzs535131/article/details/103813081