Working principle and source code interpretation based on thread pool
With the increasing number of CPU cores, it is inevitable to use multithreading technology to make full use of its computing power. Therefore, multithreading technology is a technology that service developers must master.
Thread creation and destruction involve system calls and consume system resources. Therefore, thread pool technology is introduced to avoid frequent thread creation and destruction.
There is an executors tool class in Java, which can create a thread pool for us. Its essence is to new a ThreadPoolExecutor object. Thread pool is almost a must for interview. This section describes the working principle of threadexecution in combination with the source code
1、 Thread pool creation
Let's take a look at the most complete construction method of ThreadPoolExecutor parameters:
① Corepoolsize: the number of core threads in the thread pool. In other words, even if there are no tasks in the thread pool, there will be corepoolsize threads waiting for tasks.
② Maximumpoolsize: maximum number of threads. No matter how many tasks you submit, the maximum number of working threads in the thread pool is maximumpoolsize.
③ Keepalivetime: the lifetime of the thread. When the number of threads in the thread pool is greater than the corepoolsize, if no task can be executed after waiting for keepalivetime, the thread exits.
⑤ Unit: This is used to specify the unit of keepalivetime, such as seconds: timeunit SECONDS。
⑥ Workqueue: a blocking queue in which submitted tasks will be placed.
⑦ Threadfactory: thread factory. It is used to create threads. It is mainly used to name threads. The thread name of the default factory is pool-1-thread-3.
⑧ Handler: reject policy, which will be called when the thread pool is exhausted and the queue is full.
The above parameters are used when creating thread pool. Interviewers often ask this question in the interview.
2、 Thread pool execution process
Here is a diagram to illustrate the execution flow of thread pool
When a task is submitted to the thread pool, it will first judge whether the current number of threads is less than the corepoolsize. If it is less than, a thread will be created to execute the submitted task. Otherwise, the task will be placed in the workqueue queue. If the workqueue is full, it will judge whether the current number of threads is less than the maximumpoolsize. If it is less than, a thread will be created to execute the task, otherwise, handler will be called, To indicate that the thread pool refused to receive a task.
Here jdk1 8.0_ 111 source code as an example to see the specific implementation.
1. Let's first look at the executor method of the thread pool
① : judge whether the current number of active threads is less than corepoolsize. If it is less than, call addworker to create a thread to execute the task
② : if not less than corepoolsize, the task is added to the workqueue queue.
③ : if putting into the workqueue fails, the thread will be created to execute the task. If the thread creation fails (when the current number of threads is not less than maximumpoolsize), reject (internal call handler) will be called to reject the task.
2. Let's look at the method implementation of addworker
This code is used to create a non core thread, that is, core is equal to false. Judge whether the current number of threads is greater than or equal to maximumpoolsize. If it is greater than or equal to, false is returned, that is, the thread creation in ③ mentioned above fails.
The lower half of the addworker method:
① When you create a worker object, you will also instantiate a thread object.
② Start this thread
3. Then go to the worker to see its implementation
You can see that threadfactory will be called to create a thread when creating a worker. Starting a thread in ② above will trigger the worker's run method to be called by the thread.
4. Next, let's look at the logic of the runworker method
When a thread calls runwoker, it will call the gettask method in a while loop, read the task from the workerqueue, and then execute the task. As long as the gettask method does not return null, the thread will not exit.
5. Finally, let's look at the gettask method implementation
① Let's ignore allowcorethreadtimeout. The default value of this variable is false. WC > corepoolsize determines whether the current number of threads is greater than corepoolsize.
② If the current number of threads is greater than corepoolsize, the poll method of workqueue will be called to obtain the task, and the timeout time is keepalivetime. If the keepalivetime is exceeded and the poll returns null, the while mentioned above will exit in sequence and the thread will finish executing.
If the current number of threads is less than corepoolsize, the take method of workqueue will be called to block the current thread.
The above working principle and source code interpretation based on thread pool is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.