I am a thread

Source: Internet
Author: User

First newborn calf

I was a thread, and I was born with a number: 0x3704, then led to a dim room where I found a lot of companions who were exactly like me.

My companion 0x6900 for a long time, he said to me with the vicissitudes of life: "Our thread of destiny is to deal with the package." After finishing the package, you have to come back here soon, or you may never come back. ”

My face ignorant, "parcel, what parcel?" ”

"Don't worry, you'll see right away, we don't have any idlers here." ”

Sure enough, before long, the door of the house opened, and a ferocious-looking fellow shouted, "0x3704, come out!" ”

As soon as I got out, I was stuffed with a heavy parcel with a piece of paper full of steps.

"Go on, take care of this parcel." ”

"Where to handle it?" ”

"Follow the instructions and go first to the Ready workshop." ”

Sure enough, there are instructions on the ground arrow, followed it came to a bright big room, there are many threads here, we are very nervous, as if always ready to go forward.

As soon as I came in, I heard the radio say: "0x3704, enter the workshop." ”

I hurried on, and many people were talking behind me.

"He's so lucky that he's just going to be ready to run. ”

"Does it matter?" ”

"No, you see how high the priority of others ah, alas!" ”

The front is the workshop, here is simply too beautiful, no wonder the old thread always nagging said: "If you can stay here just fine." ”

Here space, good vision, fresh air, flowers, and a lot of people have never seen, like waiters waiting for me to serve.

They also have numbers, and more importantly, everyone has a label that says: Hard disk, database, memory, Nic ...

I can't understand it now, let's take a look at the procedure.

First step: Remove the parameters from the package.

Open the package, inside there is a HttpRequest object, you can take the username, password two parameters.

Step Two: Perform the login operation.

O, originally is someone to login ah, I put username, password to the database attendant, he took the data, slowly to go.

Why is he so slow? But am I just going to be able to stay in the workshop for a little while? It's not going to be a third step anyway.

Just then, the broadcast in the workshop rang out: "0x3704, I am the CPU, remember the steps you are taking, and leave immediately with the parcel!" ”

I slowly to start cleaning up.

"Hurry up, the other threads will be coming in soon." ”

Left the workshop, and came to a large room, where there are many threads in slowly tea, playing cards.

"Man, are you all right?" ”

"You new to the bar, you do not know I am waiting for the database waiter to give me the data Ah!" It is said that they are hundreds of thousands of times times slower than we are and have a good rest here. ”

"Ah?" So slow! I have someone here to log on to the system, can wait such a long time? ”

"Rest assured, haven't you heard of the human day, CPU a year?" We are here to use nanosecond, millisecond time, the human waiting for a second, equivalent to us for several days, time. ”

Just go to sleep for a while. Do not know how long after, the Big Horn again began to broadcast: "0x3704, your data came, go to carry On!" ”

I turned to the CPU shop to run, found that the door only out of the way!

The back came bursts of laughter sound: "Really is new, do not know also have to go Ready workshop." ”

So hurried to the ready workshop, this time not so good luck, and so long before being called into the CPU workshop again.

While waiting, I heard someone whisper:

"Have you heard that a thread has been killed recently?" ”

"Why?" ”

"This guy relies on the CPU shop to keep the CPU utilization at 100%, and then he gets killed." ”

"Where did you get it after killing?" ”

"It might have been garbage collected." ”

My heart hit a shiver, hurriedly continue to deal with, the rest of the action is much faster, the second step login success.

Step three: Build the home page after the login is successful.

This step is a bit time-consuming, because there is a lot of HTML to deal with, do not know the code who wrote, processing is very annoying.

I'm making a nervous production of HTML, and the CPU is starting to call:

"0x3704, I'm the CPU, remember the steps you're taking, and leave with the package right away!" ”

"Why?" ”

"Each thread can only run on the CPU for a while, and it's time for someone else to use it, you go to the Ready shop and wait to call you." ”

In this way, I have been in the "ready-to-run" two states do not know how many times, and finally follow the list of steps to finish the work.

Finally, the package containing the HTML was sent back in a smooth way. As for what to do after login, I don't care. will be back to my dim room, really a little reluctant here. But I was lucky to have some threads, and they were completely destroyed when they finished, and I was alive!

Back to the little black House, the old thread 0x6900 asked:

"How about that?" How did you feel on the first day? ”

"Our world rules are complex, first you don't know when you will be picked to execute; second, in the execution of the process can be interrupted at any time, let out the CPU workshop; Thirdly, once the hard disk, the database such time-consuming operation, you have to let the CPU to wait; You have to wait for the CPU to pick. ”

"The lad understood well. ”

"I don't understand why many threads are dead when they finish the task, why are we still alive?" ”

"You don't know yet?" Immortality is our privilege! We have an official name here called the thread pool ! ”

Second back

A dull day is a day of heaven and earth past, as a thread, my daily life is to take parcels, deal with parcels, and then back to our Dim Home: Thread Pool.

One day when I came back, I heard a brother say, today to have a good rest, tomorrow is the craziest day. I glanced at the calendar and tomorrow is November 11.

Sure enough, 0 points just past, do not know those human how, crazy delivery package, in order to cope with the influx of massive parcels, line constructor no one can idle down, all go out to deal with packages, CPU workshop Utilization super high, hard drive in the buzz, Nic Crazy Flash, even so, or processing not finished, mountains.

We have no way, it is too much, most of these parcels are browse pages, place orders, buy, buy, buy.

Don't know how long after, parcel mountain finally slowly disappeared. Finally can catch breath, I think I will never forget this day.

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

My daily job is to handle the user's login, browse, cart, order, payment.

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

"Wait until the system restarts." "0x6900 said.

"Have you ever experienced a system restart?" ”

"How could that be?" System restart is our time of death, the end of the world, once restarted, the entire thread pool is destroyed, time and space are all gone, all from the beginning. ”

"When will it be restarted?" ”

"It's not good to say, enjoy the life in front of you ..."

In fact, life is rich and colorful, my favorite package is to upload pictures, because the network is slow, so can be in the Ready workshop, CPU workshop for a long time, you can know a lot of fun threads.

For example, the last time I met the memecached thread, he told me that he had cached a lot of user data and distributed it with his help! It's on a lot of machines!

I asked him: "No wonder the subsequent logon operation is so much faster, the original is no longer from the database to fetch data from you there, ah, hey, you are distributed you have been to other machines?" ”

He said: "How could it be!" I can only access the data by sending a GET, put command through the network to that machine every time, and I don't know anything else. ”

Like the last time I ran into a database connection thread when I was waiting, I knew he was a connection pool, almost exactly the same as our thread pool.

He told me: "Some of the packages are so perverted that they look at the order data for a year and I'm exhausted." ”

I said: "You, you that is pure data, you pass the data to me, I have to assemble into HTML, the workload does not know how much more than you." ”

He suggested to me: "You must have a good relationship with memecached, directly from him to take the data, as little as possible directly to call the database, so that our JDBC connection can also live a little easier." ”

I readily accept: "Good ah, the key is that you have to get the data in advance cache Ah, or I first ask the cache, no data, I do not have to find you?" ”

Life is like this, if you don't have a little fun yourself, what do you mean?

Third, the Escape from the tiger

A few days ago I met a terrible thing, almost died outside, not back to the thread pool. In fact, I should have been able to anticipate this distress, it was too careless.

That day I handled some of the deposits and withdrawals from the HTTP package, the old thread 0x6900 me: "Be careful when handling these parcels, you must first get a lock, when you deposit or withdraw funds to the account must be locked, otherwise the thread will be in your waiting time to swoop in, I was very coarse when I was young, and I was in a mess. ”

In order to "scare" me, the kind 0x6900 gave me two tables:

(1) no lock-in conditions

(2) lock-up condition

I see frightening, the original lock will bring such a serious accident. From then on to see the deposit, the withdrawal package is more careful, fortunately, no accident.

Today I received a package is a transfer, from a famous actor's account to a famous director's account transfer money, specifically who I will not disclose, the amount is really not small.

I follow the instructions of the old thread, must lock ah, first to the famous actor's account lock, and then to the famous director's account lock.

But what I never imagined was that there was a thread, yes, 0x7954, who was transferring money from the director's account to the actor's account.

So there's the situation:

At first I did not know what the situation, has been sitting waiting for the workshop silly, but wait too long, up to dozens of seconds! I have never experienced such an event.

That's when I saw the thread 0x7954, and he was sitting there drinking coffee, and I chatted with him:

"Dude, I think you've had 8 cups of coffee, so why don't you go to work?" ”

"Didn't you drink 9 cups of tea?" "0x7954 replied.

"I am waiting for a lock, I do not know which grandson has not been released!" ”

"I'm waiting for the lock too, if I know which grandson doesn't release the lock, I'll beat him to death!" "0x7954 no weakness.

I took a sneak peek at the guy in his arms, holding the lock of a director I was waiting for?

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

Soon the two of us quarreled with each other:

"Give me your lock first, let me finish first!" ”

"No, it's never been done before the release of the lock, now absolutely not to you!" ”

It's just a matter of seconds from the quarrel to the fight. More importantly, the two of us not only hold the lock of this famous director and actor, but also a lot of other locks, resulting in more and more threads waiting for the crowd to fill the room. Finally things really big, I have never seen the ultimate Big Boss "operating system" also came. Big Boss after all well-informed, he glanced at, grunted, very disdain to say:

"There is a deadlock again." ”

"You two will kill one, come on, come and draw the lottery." ”

This all of a sudden scare me to pee, so serious ah! I smoked the sign with trepidation, opened a look, is a "live" word. Alas, the small life finally saved.

Poor 0x7954 was forced to hand over all the resources and was unfortunately killed and disappeared. I got the director's lock and I can get to work. The big Boss "operating system" disappeared like a wind, and only his voice came from behind him:

"Remember, we have directors > actors here who have to get the director's lock in any situation." ”

Since there are not only directors and actors, but also a lot of other people, Big Boss left a table, inside is an algorithm, used to calculate the size of the resources, after calculation, always follow the way from large to small to obtain the lock:

I went back to the thread pool, and everyone knew about my adventures, and I kept asking around.

Devils's thread dispatcher pasted the algorithm of the big Boss onto the wall.

Every morning, we have to be like a moral integrity House agent, hairdressing shop attendant, standing at the door, like being played monkey loudly recite:

"Multiple resources lock to keep in mind, be sure to press the boss algorithm than the size, and then from the largest start lock. ”

Back to the lake.

Over the next few days, I and other threads have found a strange thing: the handling of parcels is getting easier, regardless of any package, whether it's logging in, browsing, saving ... The steps are all the same, returning a fixed HTML page.

Once I stole a glance, it said: "The system will be from 00:00 to 4:00 maintenance upgrades, to bring you the inconvenience we are deeply sorry!" ”

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

"Alas, our life is over, it seems that we are going to restart the system, we are going to disappear, goodbye brother." ”

The moment the system restarts has finally arrived. I saw things in the house were missing, waiting for the workshop, the Ready workshop, and even the CPU workshop was slowly disappearing. The thread brothers around me are getting less and more, and I'm the last one left.

I shouted in the open wilderness: "Is there anyone else?" ”

No answer.

Our generation thread pool has fulfilled its mission ...

But the next generation of thread pools will be reborn!

I am a thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.