Thread pool principle of Java concurrency thread pool (2)_ Power node Java college sorting

Thread pool example

Before analyzing thread pools, let's look at a simple thread pool example.

Operation results:

The example includes three main steps: creating a thread pool, adding tasks to the thread pool, and closing the thread pool. Later, we will analyze ThreadPoolExecutor from these three aspects.

Thread pool source code analysis

(1) Create thread pool

The following describes the process of creating a thread pool with newfixedthreadpool().

1. newFixedThreadPool()

Newfixedthreadpool() in executors Java. The source code is as follows:

Note: newfixedthreadpool (int nthreads) is used to create a thread pool with the capacity of nthreads. When newfixedthreadpool() calls threadpoolexecutor(), it will pass a linkedblockingqueue() object, which is a blocking queue implemented by a one-way linked list. In the thread pool, the blocking queue is used to realize "when the number of tasks in the thread pool exceeds the allowed number of tasks, some tasks will block waiting". For the implementation details of linkedblockingqueue, readers can refer to "linkedblockingqueue of Java multithreading series -" JUC set "08".

2. ThreadPoolExecutor()

Threadpoolexecutor() in ThreadPoolExecutor Java. The source code is as follows:

Note: this function is actually another constructor that calls ThreadPoolExecutor. The source code of this function is as follows:

Note: initialization is performed in the constructor of threadpoolexecutor(). The values of corepoolsize, keepalivetime and workqueue variables are known, and they are passed through newfixedthreadpool(). Let's look at threadfactory and handler objects.

2.1 ThreadFactory

The threadfactory in the thread pool is a thread factory. The creation of threads in the thread pool is completed through the thread factory object (threadfactory). The threadfactory object mentioned above is through executors Returned by defaultthreadfactory(). Executors. The source code of defaultthreadfactory () in Java is as follows:

Defaultthreadfactory() returns the defaultthreadfactory object. Executors. The source code of defaultthreadfactory () in Java is as follows:

Note: threadfactory is a thread factory that provides the function of creating threads. It provides the function of creating threads through newthread (). Let's briefly talk about newthread (). The corresponding task of the thread created by newthread() is a runnable object. The threads it creates are "non daemon threads" and "thread priority is thread. Norm_priority".

2.2 RejectedExecutionHandler

Handler is the processing handle of the reject policy in ThreadPoolExecutor. The so-called reject strategy refers to the corresponding strategy adopted by the thread pool to reject the task when the task is added to the thread pool. By default, the thread pool adopts the defaulthandler policy, that is, abortpolicy policy policy. In the abortpolicy policy policy, an exception will be thrown when the thread pool rejects a task! Defaulthandler is defined as follows:

private static final RejectedExecutionHandler defaultHandler = new AbortPolicy(); The source code of abortpolicy is as follows:

(2) Add task to thread pool

1. execute()

Execute() is defined in ThreadPoolExecutor In Java, the source code is as follows:

Note: execute() is used to add tasks to the thread pool for execution. It will be processed in three cases: case 1 - if the number of tasks in the thread pool is less than the core pool size, that is, there are less than corepoolsize tasks in the thread pool; At this point, create a new thread and add the task to the thread for execution. Case 2 -- if "number of tasks in thread pool" > = "core pool size", and "thread pool is allowed"; At this point, the task is added to the blocking queue to block waiting. In this case, the thread pool status will be confirmed again. If the thread pool status read for the second time is different from the thread pool status read for the first time, the task will be deleted from the blocking queue. Case 3 - not both. In this case, try to create a new thread and add the task to the thread for execution. If the execution fails, reject the task through reject().

2. addWorker()

The source code of addworker () is as follows:

Note: addworker (runnable firsttask, Boolean core) is used to add a task (firsttask) to the thread pool and start the task. If core is true, the boundary is corepoolsize. If "number of existing tasks in thread pool > = corepoolsize", false is returned; If the core is false, the maximumpoolsize is taken as the boundary. If "the number of existing tasks in the thread pool > = maximumpoolsize", false is returned. Addworker () will continuously try to update the CTL status through the for loop. The CTL records "the number of tasks in the thread pool and the thread pool status". After the update is successful, add the task to the thread pool through the try module and start the thread where the task is located.

From addworker (), we can clearly find that when adding a task, the thread pool will create the worker object corresponding to the task; A worker object contains a thread object. (01) by adding the worker object to the "workers collection of threads", the task can be added to the thread pool. (02) execute the task by starting the thread corresponding to the worker.

3. submit()

In addition, submit() is actually implemented by calling execute(). The source code is as follows:

(3) Turn off thread pool

The source code of shutdown () is as follows:

Note: shutdown() is used to close the thread pool.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>