This article is a superficial analysis of the java thread pool. The field of view is limited to the basic implementation of the thread pool, excluding the lifecycle and state management.
The thread pool separates the submission and execution of tasks. The advantage of doing so is that the processor can execute tasks at the same time as much as possible.
Therefore, the thread pool provides two roles:
Task
Worker of the worker performing the task
Related to this, consider the following:
Number of workers:
The consideration of the number of workers is a key to the thread pool design. Therefore, this is determined by the first two parameters provided when the thread pool is constructed.
Minimum corePoolSize
Maximum number of workers maximumPoolSize
When to dismiss a minimum number of workers with excessive employment:
When workers have redundant tasks, they will wait for the arrival of the task. If there is no task within the specified time, we will fire the redundant workers.
The specified time is the third and fourth parameters in the constructor.
Acceptable task count:
A worker can only process one task at a time. The number of workers is limited, so when the number of workers in excess comes:
Whether to accept the thread pool
Y:
The number of tasks accumulated in the waiting state is no longer allowed to accept the tasks:
Specify the capacity of the waiting task list [bound]. After the capacity is reached, the new task will not be accepted.
Unlimited acceptance of waiting tasks [unbound]
The task wait list is determined by the fifth parameter of the constructor.
Long keepAliveTime, TimeUnit unit
N:
Method of rejection:
It is determined by the seventh parameter of the constructor.
The sixth parameter determines
The factory method in which the worker executes the task thread.
Working principle:
1. when the number of workers does not reach the minimum number of workers, a worker is created in the thread pool whenever a task comes, and a new thread is created from the thread factory to use the task as the target of the thread, give the thread to the worker and add the worker to the worker set. The task is executed immediately.
2. If the current number of workers is larger than the minimum number of workers, it indicates that the number of workers has already been hired. Please wait for the job.
The thread pool tries to add the task to the task list. If the task is successful, the task submission step ends.
. The task list is a concurrent blocking queue. The worker constantly tries to get the task from the queue. if the task is obtained (see figure 5), the worker takes the task for execution. This is the task execution step.
3. If the task fails to be added to the queue, the task list is full and the task cannot be inserted into the queue.
At this time, the thread pool determines whether the number of workers has reached the maximum number of workers.
If not, it is decided to hire another worker (which is no different from the process of starting to hire a worker ).
4. If the job has reached employment launch, there is no way for the thread pool, so you have to reject the job.
The following methods are rejected:
1. Throw an exception to the task deliverer and tell him "I'm tired, I really don't do it ". -- This is too rough. The thread pool does this by default!
2. Discard the task. -- This is too cumbersome.
3. discard a recent task from the task list and put it in. -- This is awesome.
4. Return it to the task deliverer and let him do it by himself. -- This is shameless.
5. There are two policies for the thread pool to allow workers to wait for tasks (this article does not consider the lifecycle and status ):
Dead! When the number of workers is less than or equal to the minimum number of employees, they will have to die if they have no tasks to do.
If no task arrives within a given period of time, the thread pool starts to count heads. If no more than the minimum number of workers are hired, the worker has to wait for a given period of time.
6. Otherwise, the thread pool ends the worker thread and tells the currently waiting worker that you have no task. Don't wait.
7. The workers who have not obtained the task are removed from the workers collection-the workers are officially laid off.