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 understand the working principle of thread pool

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to understand how thread pools work". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand how thread pools work.

What is a thread pool?

"Little Field snail" is diligent, hard-working and working day and night. Finally, one day, he was promoted to the head of the company and was in charge of the daily business of the company.

On a light day, the boss found Xiao Tian snail. "there are more and more employees in our company. I want to set up an 'employee management system'. Make arrangements over there, ha. It will be finished in a month." Small field snail patted the chest, no problem!

Because there are no programmers in the company, Ota quickly opens the "Zhu Bajie net" and submits the requirements of the employee management system. Soon, the "developer (name, thread A)" takes the order, negotiates the contract, starts development, and delivers the system. A series of processes came down, and a month later, an employee management system with five internal organs was finally completed. The boss spoke highly of it.

Soon after, the boss said again, "more and more people in the company are late. Let's set up an attendance system." Xiao Tian snail received the task, immediately began to go to Zhu Bajie net, raised the demand to find someone to develop, this time came to "thread B to pick up the order."

Like the dead, at the end of the month, the boss proposed to develop a salary system. Oda snail listened to the scalp tingling, one day day, repeated to the Internet to find someone to develop! "in order to save costs, why don't we hire a few programmers (thread areb) and set up our own IT technical department? let's call the IT department" thread pool "!" When the boss heard this, he hit it off!

A thread pool is a pool that manages threads. When there is a task to deal with, you don't have to create new threads frequently, but take a thread out of the pool to process it. When the task is finished, the thread is not destroyed, but is waiting for the next task. Therefore, it can save resources and improve the response speed.

What is a core thread?

After the establishment of the "thread pool" IT department, it hired several employees who have formal contractual relations with the company, and "Little Tian snail" managed their regular employees to be the "core thread". When the boss puts forward a request, Oda assigns the request to the thread with no work on hand.

What is a blocking queue?

One morning, the boss was sleepy. After I came to the company, I mentioned the "four needs" in one breath. After completing the task, I found that there was still one requirement task left. How do you arrange this? Are you going to "Zhu Bajie part-time Network" to find someone again? Set up the "thread pool IT department", but also to find people (looking for thread work), will be laughed out of the teeth!

The clever little snail came up with a good idea. We can set up a DPMS demand pool, put the unallocated requirements into the DPMS demand pool to be completed, and wait until the first one finishes the work, and then take the task away. This DPMS demand pool, let's name it "blocking queue", or "WorkQueue" in English.

What is a non-core thread?

On another sunny afternoon, the boss drank a cup of coffee and had nothing to do at leisure, so he ran to the "blocking queue" (DPMS demand pool) to have a look. The demand pool has piled up dozens of demands, and the schedule is full. The boss immediately called "Xiao Tian snail into the office" to discuss how to deal with these demand tasks.

"how about we hire more employees (how many more" core threads ")? "No, no, the company's financial expenses are a little bit high!"

"otherwise, we ask the business to ask for less task requirements? (" request less ")" are you stupid, asking for less, aren't you cutting off your own money? you go home and think of a way! " The boss magnified his voice.

The snail went home to close his eyes and went to bed early every day, not hearing anything outside the window. Finally, one day, in a dream incense, he came up with a good idea.

"Boss, we can go to another company (the" outsourcing company ") to hire a few employees (suppose the name is dforce eforce fjigg) for a period of time and let them do the requirements in the DPMS demand pool (blocking queue). Wait until the requirements are done and then send them back." As soon as the boss heard this, he was happy, and he was delighted: "someone has done the work in need, and the financial staff of the company can save money and have the best of both worlds." ~ these outsourced employees sent in (dphamel), let's call it a "non-core thread".

What is free time?

Since the arrival of the outsourced staff ("non-core thread"), the boss has breathed a sigh of relief that someone has finally done so much work.

However, one day, at the so-called off-duty time at 7 o'clock, the boss walked out of the office and found that the employees of the "thread pool" IT department were all gone. In a heart angry: "these pink sausages, how to get off work on the run, the work is so unsaturated"? He casually clicked into the DPMS demand pool and found that the original requirements had been done. There are still a lot of outsourced colleagues (non-core threads) to get paid. This wave of losses is huge.

The next day, Little Tian snail was secretly called into the boss's office, since the DPMS demand pool is no longer needed. Shall we send our outsourced colleagues (non-core threads) back? However, generally speaking, as soon as there is no demand, let them go back immediately ("thread recycling"). If the demand comes back all of a sudden, it will be a bit hold.

"Why don't we wait for the demand pool to be empty every other 15 days or 10 days, and then let our outsourcing colleagues (" non-core threads ") go back?" This definition of 15 days or 10 days is the "thread idle survival time".

What is saturation strategy?

In the approach of Singles Day holiday, not only the boss put forward a lot of demand, but also the new operators raised a lot of demand. New demand, such as the source of running water, rolling in ~

First of all, in the "thread pool" IT department, three regular employees (core threads) are busy dealing with requirements ("requests"), then the DPMS requirement pool ("blocking queue") is also crowded, and finally, even dQuery eGraine ("non-core threads") are busy with outsourcing colleagues.

At this time, the demand is still endless, what should we do? Are you in a hurry to launch Singles Day holiday? The small field snail has a sad face, from the rising tide to the ebb.

There is no choice but to use the "saturation strategy". Such as "discard requirements tasks"? "throw an exception and tell the boss not to add requirements"? "discard the oldest requirement task in the demand pool"? Or "leave it to those who ask for it"?

Finally, the boss decided, "refuse to mention new requirements", so the "thread pool" IT department is still running normally.

There are four main types of saturation strategy events in online cities.

AbortPolicy (throws an exception, default)

DiscardPolicy (newly submitted tasks are directly abandoned)

DiscardOldestPolicy (discard the oldest task in the queue and continue to submit the current task to the thread pool)

CallerRunsPolicy (leave it to the thread in which the thread pool calls are located for processing, that is, to roll back some tasks to the caller)

Thread Pool working principle flow Chart & Source Code Overview

At the end of the story, let's review the next line pool work flow chart.

Interested friends, you can also take a look at the source code.

If (command = = null) throw new NullPointerException (); int c = ctl.get (); / / determine whether the current number of active threads is less than corePoolSize if (workerCountOf (c) < corePoolSize) {/ / if so, call addWorker to create a thread to execute the task if (addWorker (command, true)) return; c = ctl.get () } / / if greater than or equal to corePoolSize, the task is added to the workQueue queue. If (isRunning (c) & & workQueue.offer (command)) {int recheck = ctl.get (); if (! IsRunning (recheck) & & remove (command) reject (command); else if (workerCountOf (recheck) = = 0) addWorker (null, false) } / / if the workQueue queue fails, create a non-core thread to execute the task else if (! addWorker (command, false)) / / (if the thread creation fails at this time (when the current number of threads is greater than or equal to maximumPoolSize)) call reject to reject the task reject (command) At this point, I believe you have a deeper understanding of "how to understand how thread pools work". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report