当前位置:网站首页>I am a thread (Revised)

I am a thread (Revised)

2022-06-21 20:33:00 Minimalist lesson

The first time A newborn calf

I'm a thread , I was born with a number :0x3704, Then he was led to a dark room , Here I found many companions who were exactly like me .

My companions 0x6900 Stay for a long time , He said to me with a voice of vicissitudes :“ Our thread's destiny is to handle packages . You have to come back here as soon as you finish handling the package , Otherwise, you may never come back .”

I look confused ,“ The parcel , What package ?”

“ Don't worry , You'll see in a minute , We don't keep idle people here .”

Sure enough , It didn't take long , The door of the house opened , Roared a ferocious looking fellow :“0x3704 , come out !”

As soon as I came out, I was stuffed with a heavy package , It also comes with a paper full of operation steps .

“ Go to the , Dispose of this package .”

“ Where to deal with ?”

“ Follow the instructions , Go to the ready workshop first .”

Sure enough , There are arrows on the ground , Followed it to a big bright room , There are already many threads here , Everybody's nervous , Seems to be ready to rush forward .

I just came in , I heard the radio say :“0x3704, Enter the workshop .”

I hurried forward , There are many people talking about .

“ He's so lucky , Just enter the ready state to run .”

“ Does it matter ?”

“ No , You see how high the priority of others is , alas !”

Ahead is the workshop , It's so beautiful here , No wonder the old thread is always nagging :“ If only I could stay here all the time .”

There's a lot of space here , Nice view , The air is fresh , Charactizing a fine spring , There are many people who have never seen , Wait for me like a waiter .

They also have numbers , More importantly, everyone has a label , It reads : Hard disk 、 database 、 Memory 、 network card ……

I can't understand now , Look at the steps .

First step : Take the parameters out of the package .

Open the package , There's a HttpRequest object , You can get it. userName、 password Two parameters .

The second step : Perform login operation .

Mr. , It turns out that someone wants to log in , I put userName、password Give it to the database attendant , He took the data , Walked slowly .

Why is he so slow ? But can I just stay in the workshop for a while ? You can't do step three anyway .

At this moment , The radio in the workshop rang :“0x3704, I am a CPU, Remember the steps you are taking , Then immediately leave with the package !”

I slowly began to tidy up .

“ Hurry up , Other threads are coming in soon .”

Leave this workshop , Come to a big room again , There are many threads drinking tea slowly , Playing CARDS .

“ Brother , You have nothing to do ?”

“ You're new here , You don't know I'm waiting for the database server to give me the data ! It is said that they are hundreds of thousands of times slower than us , Have a good rest here .”

“ ah ? So slowly ! I have someone logging in to the system , Can you wait so long ?”

“ don 't worry , You haven't heard of a human day ,CPU A year ? We use nanoseconds here 、 Millisecond timing , Wait a second , It's equivalent to us for several days , In time .”

Just get some sleep . I don't know how long , The big horn began to broadcast again :“0x3704, Here's your data , Go and execute !”

I turned and went to CPU Workshop run , I found that the door here can't get in or out !

Bursts of laughter came from behind :“ Sure enough, it's a newcomer , I don't know. I have to go to the ready workshop and wait .”

So I hurried to the ready Workshop , Not so lucky this time , It took a long time to be called into CPU workshop .

While waiting , I heard someone whispering :

“ Have you heard that , Recently, a thread was kill It fell off .”

“ Why ?”

“ This guy is hanging on CPU The workshop doesn't go , hold CPU The utilization rate has been 100%, Then they were kill It fell off .”

“Kill Where did it go after it fell ?”

“ It may have been recycled .”

I shivered in my heart , Get on with it , The rest is much faster , Step 2 login succeeded .

The third step : Build the home page after successful login .

This step is a little time-consuming , Because there are so many HTML Need to deal with , I don't know who wrote the code , It's annoying to deal with .

I'm busy making HTML Well , CPU Started barking again :

“0x3704, I am a CPU , Remember the steps you are taking , Then immediately leave with the package !”

“ Why ?”

“ Each thread can only be in CPU Run on for a while , When the time comes, you have to let others use it , You go to the ready workshop and stay , Wait to call you .”

That's it , I've been here “ be ready —— function ” I don't know how many times I rotate in these two states , Finally, I finished the work according to the step list .

Finally, successfully include html My package was sent back . As for what to do after logging in , I don't care . I'm going back to my dark room soon , I'm a little reluctant to be here . But compared with some threads , I'm still lucky , They were completely destroyed after running , And I'm still alive !

Back to the little black room , Old thread 0x6900 ask :

“ What about? ? How did you feel on the first day ?”

“ The rules of our world are complex , First of all, you don't know when you will be selected to perform ; second , May be interrupted at any time during the execution , Give up CPU workshop ; Third , Once the hard disk appears 、 Database is such a time-consuming operation , You have to make way CPU To wait ; Fourth , That's the data , You don't have to do it right away , We have to wait CPU choose .”

“ The young man understands well .”

“ I don't understand why many threads die after performing tasks , Why are we still alive ?”

“ You don't know ? Immortality is our privilege ! We have a formal name here , called Thread pool !”

The second time get

The plain days passed day by day , As a thread , My daily life is to pick up packages 、 Handling packages , Then go back to our dark home : Thread pool .

One day when I came back , Heard a brother say , Have a good rest today , Tomorrow is the craziest day . I glanced at the calendar , Tomorrow is 11 month 11 Number .

Sure enough , It's just past zero , I don't know what happened to those humans , Crazy delivery of packages , In order to cope with the massive influx of packages , No one in the thread pool can be idle , Go out and deal with the package ,CPU The utilization rate of workshop is very high , The hard drive is buzzing , Crazy flash of network card , even so , Still can't finish , pile up like a mountain hand .

We have no way , It's just too much, too much , Most of these packages are browsing pages , Place the order , buy 、 buy 、 buy .

I don't know how long , Baoshan finally disappeared slowly . Finally be able to breathe , I think I will never forget this day .

Through this event , I understand the world I live in : This is an e-commerce website !

My daily job is to handle the login of users , Browse , The shopping cart , Place an order , payment .

I asked the elder of thread pool 0x6900:“ When are we going to work ?”

“ Wait until the system restarts .”0x6900 say .

“ Have you ever experienced a system restart ?”

“ How is that possible? ? System restart is our moment of death , The end of the world , Once the restart , The entire thread pool is destroyed , Time and space all disappear , All over again .”

“ When will it restart ?”

“ It's hard to say , Enjoy the life in front of you ……”

In fact, life is rich and colorful , My favorite package is uploading pictures , Because the network is slow , So it can be in the ready Workshop 、CPU Stay in the workshop for a long, long time , You can know many interesting threads .

For example, last time I met memecached Threads , He told me that with his help, he cached a lot of user data , Or distributed ! Many machines have !

I asked him :“ No wonder the login operation was so fast later , It turns out that you don't get data from the database anymore. You have it there , Hey, by the way, you are distributed. Have you ever been to other machines ?”

He said :“ How is that possible? ! I can only send one to that machine through the network at a time GET、PUT Command to access data , I don't know anything else .”

For example, the last time I encountered a database connection thread while waiting , Only then did I know that there was also a connection pool , It is almost the same as our thread pool .

He told me :“ Some packages are too sick , Even check the order data of one year , It's killing me .”

I said, :“ Pull it down. You , That's pure data , After you pass the data to me , I have to assemble HTML, I don't know how many times the workload is greater than you .”

He suggested that I :“ You must be with memecached Good relations , Get the data directly from him , Try not to call the database directly , So that we JDBC connection Can also live a little easier .”

I gladly accept :“ Good good , The key is that you have to get the data into the cache in advance , Or I'll ask you first , No data , Don't I have to find you now ?”

Life is like this , If you don't have some fun yourself , What's the point ?

The third time Escape from danger

Something terrible happened to me a few days ago , Almost died outside , Can't go back to the thread pool . In fact, I should be able to foresee this distress , It's so careless .

That day I dealt with something from http Deposit and withdrawal packages sent , Old thread 0x6900 Specifically asked me to :“ Be very careful when handling these packages , You must first get a lock , Be sure to lock your account when you deposit or withdraw money from it , Otherwise, other threads will sneak in while you wait , Sabotage , I was rough when I was young , Just poked the basket .”

in order to “ intimidate ” I , Well intentioned 0x6900 And gave me two forms :

(1) Without locks

(2) Locking situation

I was terrified to see , It turned out that not locking would bring such a serious accident . From then on, I see deposits 、 Take extra care when you withdraw the package , Fortunately, there has been no accident .

A parcel I received today is a transfer , Transfer money from the account of a famous actor to the account of a famous director , I won't reveal who it is , The amount is really not small .

I followed the old thread's instructions , It must be locked , First lock the accounts of famous actors , Then lock the account of the famous director .

But what I never thought was , There's another thread , Yes , Namely 0x7954, At the same time, he is transferring money from the director's account to the actor's account .

So , That's what happened :

At first I didn't know what was going on , I've been sitting waiting in the workshop waiting for , But the waiting time is too long , For tens of seconds ! I've never experienced such an event .

Then I saw the thread 0x7954 , He sat there leisurely drinking coffee , I talked to him :

“ Brother , I think you've drunk 8 A cup of coffee , Why don't you go to work ?”

“ You don't drink 9 Have you had a cup of tea ?”0x7954 He replied .

“ I'm waiting for a lock , I don't know which grandson hasn't been released !”

“ I'm waiting for the lock, too , If I know a grandson who doesn't release the lock, I'll beat him to death !”0x7954 Show no weakness .

I took a sneaky look , Isn't this guy holding the lock of a director I'm waiting for ?

Obviously ,0x7954 I also found that I was holding the lock he was waiting for .

Soon we two quarreled , To outdo each other :

“ Give me your lock first , Let me finish first !”

“ no way , Always release the lock after finishing the work , I can't give it to you now !”

From quarrel to fight , Just a few seconds . what's more , We don't just hold the lock of this famous director and actor , There are many other locks , Causing more and more threads to wait , The house was crowded with onlookers . Finally, things really made a big deal , I've never seen the ultimate big boss“ operating system ” Here we are . Big Boss After all, I've seen a lot , He took a look at , Hum , Say with disdain :

“ Deadlock again .”

“ You two are going to Kill Drop one , Come on , Come and draw lots .”

This scared me to pee , It's so serious ! I drew the lot tremblingly , Open it up , It's a “ live ” word . alas , My life was finally saved .

pitiful 0x7954 After being forced to hand over all the resources , Unfortunately, I was kill fall , Vanished . I got the director's lock , You can start working . Big Boss“ operating system ” Disappeared like a gust of wind , Only his voice came from behind :

“ remember , Our director here > actor , In any case, first get the director's lock .”

Because it's not just directors and actors , There are many others , Big Boss Left a form , Inside is an algorithm , Used to calculate the size of resources , After the calculation , Always get the lock from big to small :

I go back to the thread pool , Everyone knows my adventure , Keep asking around me .

The evil dispatcher Boss The algorithm is pasted on the wall .

Every day in the morning , We all have to be like unscrupulous housing agents 、 Like a waitress in a beauty salon , Stand at the door , Recite loudly like a monkey :

When locking multiple resources, remember , Be sure to press Boss The algorithm is smaller than , Then lock it from the largest one .

The fourth time Farewell to rivers and lakes

Many days later , I and other threads found a strange thing : The handling of packages is becoming easier and easier , No matter what package , Whether it's logging in 、 Browse 、 Deposit money …… The processing steps are the same , Return to a fixed html page .

Once I took a sneaky look , It reads :“ The system will be launched tonight 00:00 to 4:00 Perform maintenance upgrades , We are deeply sorry for the inconvenience caused to you !”

I went to tell the old thread 0x6904, He sighed and said :

“ alas , Our lives have come to an end , It seems that the system will be restarted soon , We're going to disappear , Goodbye, brother .”

The moment of system restart finally came . I saw the things in the house disappear one by one , Wait for the workshop 、 Ready Workshop , even to the extent that CPU The workshops are slowly disappearing . There are fewer and fewer thread brothers around me , Finally, I was left alone .

I shouted in the open field :“ Anyone else ?”

Nobody answers .

Our generation of thread pools has accomplished its mission ……

However, the next generation thread pool is about to be reborn !

原网站

版权声明
本文为[Minimalist lesson]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211842021930.html