Qu Wen: I am a thread and Qu wen I am a thread. Quwen: I am a thread, Quwen I am a thread from: coderising (number: coderising) Author: IBM Liu Xin I am a thread, when I was born, I was programmed with a number: (conversion) Quwen: I am a thread, Quwen I am a thread
From: codenon turning over (No.: coderising)
Author: IBM Liu Xin
I am a thread. when I was born, I was coded with a Number: 0x3704. then I was taken to a dark room where I found many of my companions.
My companion 0x6900 stayed for a long time. he said to me with vicissitudes of life:
The fate of our thread is to process packages. After the package is processed, you have to return to here immediately. Otherwise, the package may never be returned.
What kind of package do I have?
"Don't worry, you will understand immediately. we don't have any idle people here. "
Sure enough, it wasn't long before the door opened. a fierce guy shouted:
"0x3704, come out! "
As soon as I came out, I was stuffed with a heavy package with a piece of paper filled with Operation steps.
"Go and handle the package. "
"Where to handle"
"Follow the instructions, first to the Ready workshop"
Sure enough, there was an indicator arrow on the ground and followed it to a bright big room. there were already many threads, and everyone was very nervous, as if they were always ready to rush forward.
As soon as I arrived, I heard the broadcast saying, "0x3704, enter the workshop"
I hurried forward, and many people behind me commented:
"He is so lucky to be able to run as soon as he enters the ready state."
"Is there a relationship? "
"No. how high is the priority of people?"
The front side is the workshop, which is very beautiful. no wonder the old thread is always nagging: it would be nice to stay here all the time.
There is a lot of space, a good view, fresh air, birds and flowers, and there are many people you have never seen before, waiting to serve me like a waiter.
They also have numbers. More importantly, each person has a tag that says: hard disk, database, memory, nic...
I cannot understand it now. let's take a look at the Operation steps:
Step 1: retrieve parameters from the package
Open the package and there is an HttpRequest object. you can obtain the userName and password parameters.
Step 2: Perform the logon operation
Sorry, someone was logged on. I handed the userName/password to the database attendant. he took the data and walked slowly.
Why is it so slow? But can I stay in the workshop for a while? However, you cannot perform step 3.
At this moment, the broadcast in the workshop rang:
"0x3704, I am the CPU. remember the steps you are performing and take the package away immediately"
I started to clean up slowly
"Hurry up, other threads are coming in soon"
After leaving the workshop, I came to a big room, where I was drinking tea and playing cards slowly.
"Buddy, you have nothing to do? "
You are new here. you don't know that I'm waiting for the database attendant to give me data! They are said to be 100,000 times slower than us. please take a rest here."
"Ah? So Slow? Someone is logging on to the system. can it take so long"
"Don't worry, have you ever heard of a human Day, CPU for a year? we use a millisecond-to-millisecond timer here. waiting for one second is equivalent to waiting for several days."
Just sleep for a while. I don't know how long it will take, and the Big Horn starts broadcasting again:
"0x3704, your data is coming. run it now"
I turned around and ran to the CPU workshop. I found that only the employees here could not go in!
The following burst of laughter:
"It's really a newcomer, but I don't know I have to wait in the ready workshop."
So hurry to the Ready workshop, this time not so good luck, waited for a long time was called again into the CPU workshop.
While waiting, I heard someone whisper:
"Have you heard that a thread has been killed recently?"
"Why? "
"This guy doesn't leave in the CPU workshop. he keeps making the CPU usage 100% and is killed."
"Where can I get it after Kill?"
"It may have been recycled."
I entered a cold greeting in my heart, followed by handling it. I received more action blocks and successfully logged on to the server in step 2.
Step 3: build the homepage after successful login
This step is a little time-consuming, because there are a lot of HTML needs to be processed, and I don't know who wrote the code, it's annoying to process it.
I am nervous about making html, and the CPU has started to be called:
"0x3704, I am the CPU. remember the steps you are performing and take the package away immediately"
"Why?"
"Each thread can only run on the CPU for a period of time, so it will have to be used by others. you can go to the Ready workshop and wait for you."
In this way, I have been in the ready-running status, and I don't know how many rotations I have made. Finally, I have finished the installation step list.
Finally, we smoothly sent the html-included package back.
I don't care what to do after I log on.
I am about to return to my dark room.
But I am lucky to have some threads. they are completely destroyed after running, and I am still alive!
Back to the dark room, the old thread 0x6900 asked:
"How? What do you feel on the first day? "
"Our world rules are very complicated. First, you don't know when the execution will be initiated; second, the execution may be interrupted at any time, so that the CPU workshop can be taken out;
Third, once a hard disk occurs, the time-consuming operations such as the database also need to let out the CPU and wait. fourth, if the data comes, you may not execute it immediately and you have to wait for the CPU to select"
"The young man understands it well."
"I don't understand why I am dead after multiple threads are executed. why are we still alive? "
You still don't know. immortality is our privilege. here we have a formal name called Thread pool! "
The plain days are like this. as a thread, every day I take packages, process packages, and return to our dark home: thread pool.
One day when I came back, I heard a brother say that today we have to take a good rest. tomorrow is the crazy day.
I took a look at the calendar. tomorrow is July 22, November 11.
Sure enough, just after, I don't know what's wrong with those humans. crazy shipping packages. in order to cope with the surging volumes of packages, no one in the thread pool can handle all the packages, CPU workshop utilization rate is very high, hard drive is buzzing, Nic crazy flash, even so, still can't finish, accumulation of mountains.
There are too many packages. most of these packages are webpage browsing, placing orders, buying, buying, and buying.
I don't know how long it will take for the parcel Mountain to disappear.
I think I will never forget this day.
Through this incident, I understand my world: this is an e-commerce website!
My daily job is to process user login, browsing, shopping cart, order, and payment.
I asked the veteran 0x6900 of the thread pool: "When will we work? "
"Wait until the moment the system is restarted," said 0x6900.
"Have you ever experienced system restart? "
"How is it possible? System restart is the time of our death, that is, the end of the world. Once the thread pool is restarted, all the threads are destroyed, the time and space disappear, and everything starts from scratch"
"When will it be restarted? "
"That's hard to say. please enjoy your life ....."
In fact, life is rich and colorful. My favorite package is to upload pictures. because of the slow network, I can stay in the ready workshop for a long time. I can know a lot of interesting threads.
For example, the last time I met the memecached thread, he told me that he cached a lot of user data and it was still distributed! There are many machines!
I said no wonder that later login operations were so much faster. it turned out that you didn't get data from the database anymore. you did. By the way, you are distributed. Have you ever been to another machine?
He said that I can only send a GET to that machine through the network each time and PUT the command to access the data. I don't know anything else.
For example, when I encountered a database connection thread while waiting, I realized that it is also a connection pool, which is almost the same as our thread pool.
He said that some packages were so abnormal that I was exhausted when I checked the order data for one year.
Let me pull you down. you are pure data. after you pass the data to me, I have to assemble it into HTML. I don't know how many times the workload is bigger than you.
He said that you must have a good relationship with memecached, take data directly from him, and call the database as few as possible. we can also make JDBC connection easier.
I said yes. The key is that you have to cache the data in advance. Otherwise, I will first ask you about the cache and there is no data. Do I have to contact you?
Life is like this. what do you mean if you don't have fun?
One day I encountered a terrible thing. I almost died outside and couldn't return to the thread pool ......
In fact, I should have been able to anticipate this distress.
A few days ago, I handled some deposit and withdrawal packages sent from http. the old thread 0x6900 specifically told me:
"Be very careful when handling these packages. you must first get a lock and lock your account when you deposit or withdraw money from your account, otherwise, other threads will be thrown in while you are waiting, causing damages. when I was young, I was very rough and stabbed"
To "intimidate" me, the kindly 0x6900 gave me two tables:
1. no locks
2. locking
I was so scared that no locks would cause such a serious accident.
From then on, I saw the deposit, and the withdrawal of the package was more careful. Fortunately, there was no accident.
One of the packages I received today is a transfer. I transferred money from an account of a famous actor to a famous director. I will not reveal the specific details, but the amount is really small.
As instructed by the old thread, I must lock the account of the famous actors first and lock the account of the famous director.
But what I never expected was that there was another thread, right, that is, 0x7954. at the same time, I transferred money from this director to this actor.
As a result, there is such a situation:
At the beginning, I still don't know what the situation is. I 've been waiting for the workshop to wait, but it took so long as dozens of seconds! I have never experienced such an event.
At this time, I saw thread 0x7954. he sat there leisurely and drank coffee. I chatted with him:
"Dude, I think you have already had 8 cups of coffee. why don't you go to work ?"
"Have you stopped drinking 9 cups of tea ?" 0x7954.
"I am waiting for a lock and I don't know which grandson will never release it"
"I am waiting for a lock. if I know which grandson will not release the lock, I will not beat him." 0x7954 is not weak.
I gave a sneak peek. Isn't this guy holding the lock of a director I'm waiting for in his arms?
Obviously, 0x7954 also found that I was holding the lock he was waiting.
Soon the two of us had a quarrel and were not mutually compatible:
"Give me your lock first and let me finish it first"
"No, I never release the lock after I finish my work. I cannot give it to you now"
From quarrel to fight, that's just a few seconds.
More importantly, we not only hold the locks of the famous director and actors, but also many other locks, resulting in more and more waiting threads. onlookers crowded the room.
The last thing is really big. I have never seen the ultimate boss "operating system.
After all, the big Boss was knowledgeable. he gave a glance and gave a sigh of relief:
"Deadlocked again"
"You two need to Kill one. come on, come and draw lots"
This scared me out of breath. it's so serious!
I drew a signature and opened it. it was a "active" word.
Alas, I finally saved my life.
After being forced to hand over all the resources, the poor 0x7954 was unfortunately killed and disappeared.
I got the director's lock and I can start working.
The big Boss operating system disappears like a burst of wind, and only his voice is heard behind him:
Remember, our director> actors must first get the director's lock, regardless of their situation.
Because there are not only directors and actors, but also many others, the Boss leaves a table with an algorithm used to calculate the size of resources. after calculation, always obtain the lock in the ascending or smallest way:
I went back to the thread pool, and everyone knew my adventure.
The fierce thread dispatcher pasted the big Boss algorithm on the wall.
Every morning, we have to stand at the door like a dry house intermediary, a beauty salon waiter, and recite it as loudly as a monkey:
"Remember to lock multiple resources by comparing the Boss algorithm and then starting from the maximum"
--------------------------------------------------------
After many days, I and other threads found a strange thing: the handling of packages is getting simpler and simpler.
Regardless of any package, whether it is login, browsing, saving..., the process is the same, return a fixed html page
One time I gave a sneak peek and said:
"The system will be upgraded from to tonight. We are sorry for any inconvenience this may cause"
I told the old thread 0x6904. He sighed and said:
"Alas, our life is over. it seems that we are about to restart the system, and we are about to disappear. goodbye, bro. "
The moment the system was restarted has finally arrived.
I saw things in the room disappear one by one, waiting for the workshop, Ready workshop, and even the CPU workshop to slowly disappear.
There are fewer and fewer thread brothers around me, and I am the only one left.
I shouted in the open wilderness: Is there anyone else?
No response.
Our generation of thread pools has completed our mission.
The next generation thread pool will soon be reborn.
(End)
If you have read this article, please try again. if there are any errors in this article, please note.
Learn from each other and make progress together!
Http://www.bkjia.com/PHPjc/1121387.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1121387.htmlTechArticle (turn) Quwen: I am a thread, Quwen I am a thread from: codenong turn (number: coderising) Author: IBM Liu Xin I am a thread, when I was born, I was assigned a number :...