Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the working principle and source code of thread pool

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to analyze the working principle and source code of thread pool, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

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 server developers must master.

The creation and destruction of threads involve system calls, which consumes system resources, so thread pool technology is introduced to avoid frequent thread creation and destruction.

There is an Executors utility class in Java that can create a thread pool for us, which is essentially new a ThreadPoolExecutor object. Thread pool is almost a required question for the interview. Combined with the source code, talk about how ThreadExecutor works.

Thread pool creation

First, take a look at the construction method with the most complete parameters of ThreadPoolExecutor:

① corePoolSize: the number of core threads in the thread pool. To put it bluntly, even if there are no tasks in the thread pool, there will be corePoolSize threads waiting for tasks.

② maximumPoolSize: the maximum number of threads. No matter how many tasks you submit, the maximum number of worker 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 corePoolSize, if there is no task to execute 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 into which submitted tasks will be placed.

⑦ threadFactory: thread factory, used to create threads, mainly to name threads, the default factory thread name: pool-1-thread-3.

⑧ handler: reject policy, which is called when the thread in the thread pool is exhausted and the queue is full.

These are the parameters used to create a thread pool, which is often asked by an interviewer during an interview.

II. Thread pool execution process

Here is a diagram to illustrate the execution flow of the thread pool.

When the task is submitted to the thread pool, it will first determine whether the current number of threads is less than corePoolSize, if so, create a thread to execute the submitted task, otherwise put the task into the workQueue queue, if the workQueue is full, determine whether the current number of threads is less than maximumPoolSize, if so, create a thread to execute the task, otherwise handler will be called to indicate that the thread pool refuses to receive the task.

Here take the source code of jdk1.8.0_111 as an example, take a look at the specific implementation.

1. First take a look at the executor method of thread pool

①: determine whether the current number of active threads is less than corePoolSize. If so, call addWorker to create a thread to execute the task.

②: if not less than corePoolSize, add the task to the workQueue queue.

③: if the workQueue fails, a thread is 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 refuse to accept the task.

2. Take a look at the method implementation of addWorker.

This code is when a non-core thread is created, that is, core equals false. Determine whether the current number of threads is greater than or equal to maximumPoolSize, and return false if it is greater than or equal to, that is, the failure to create a thread in ③ mentioned above.

The second half of the addWorker method:

① creates a Worker object and also instantiates a Thread object.

② start start this thread

3. Go to Worker to see its implementation

You can see that threadFactory is called when the Worker is created to create a thread. Starting a thread in the ② above triggers the run method of Worker to be called by the thread.

4. Next, let's look at the logic of the runWorker method.

When the thread calls runWoker, the while loop calls the getTask method to read the task from the workerQueue and then executes the task. This thread does not exit as long as the getTask method does not return null.

5. Finally, let's take a look at the getTask method implementation.

① Let's forget about 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 is called to get the task, and the timeout is keepAliveTime. If the length of the keepAliveTime is exceeded, poll returns null, and the while mentioned above exits sequentially and the thread is finished.

If the current number of threads is less than corePoolSize, the take method of workQueue is called to block the current.

This is the answer to the question about how to analyze the working principle of the thread pool and the source code. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report