"Turn" a story to help you understand threads and thread pools

Source: Internet
Author: User

Original: http://www.code123.cc/2486.html

It's really interesting ~ ~ Read Comprehension can also be a bit deeper

I was a thread, I was born a number: 0x3704, and then led to a dim room, here I found a lot of my identical companions.

My companion 0x6900 for a long time, he said to me with vicissitudes of breath:

Our thread's destiny is to deal with the parcel. After finishing the package, you have to come back here soon, or you may never come back.

My face is ignorant, parcel, what parcel?

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

Sure enough, not long before the door of the house opened, 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 deal with"

"Follow the instructions, 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, get into the workshop."

I hurried forward, a lot of people behind the argument said:

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

"Does it matter?" “

"No, you see how high the priority is, 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: HDD, 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, there is a HttpRequest object, you can take to UserName, password two parameters

Step two: Perform a login operation

AO, originally is someone to login ah, I put username/password to the database waiter, he took data, slowly away.

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, take the parcel away immediately"

I slowly start packing.

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

Leave this workshop, and come to a large room, where many threads slowly in tea, playing cards.

"Man, are you all right?" “

"You new to the put, 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, you have not heard of the human day, CPU a year, we are here with nanosecond, millisecond time, the human waiting for a second, equivalent to us for several days, and"

Just sleep for a while, do not know how long after the Big Horn again began to broadcast:

"0x3704, your data is coming, go ahead."

I turned to the CPU shop to run, and found that the only out of here!

The sound of laughter came from behind:

"It's a new guy, I don't know, I have to get ready."

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'd you get it after kill?"

"Might have been garbage collected."

My heart hit a shiver, hurriedly continue to deal with, accept the action block more, the second step login succeeded

Step three: Build the home page after successful login

This step is a bit of time, 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 has started to call:

"0x3704, I am the CPU, remember the steps you are taking, take the parcel away immediately"

"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 ready-run these two states, do not know how many times the rotation, finally installed the list of steps to finish the work.

Finally, the package containing the HTML was sent back.

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'm lucky to have some threads, and they're completely destroyed when they're done, and I'm still 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 of all you don't know when you will be picked to execute; Second, in the execution of the process may be interrupted at any time, let out the CPU workshop;

Thirdly, once the hard disk is present, the time-consuming operation of the database has to be out of the CPU, waiting; The data comes, you don't have to do it right away, and you have to wait for the CPU to pick "

"Well, that's a good idea."

"I don't understand why many threads are dead when they're done, why are we still alive?" “

"You don't know that immortality is our prerogative, we have a formal name here, called the thread pool!" “

The dull days were so day by day, as a thread, my daily life was to take the parcel, deal with the parcel, and then return 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 packages 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?" "

"To wait until the system restarts," 0x6900 said.

"Have you ever experienced a system restart?" "

"How could that be?" , the system restart is our time of death, that is, 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, colorful life, 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 memecached thread, he told me to cache a lot of user data through him, or distributed! It's on a lot of machines!

I said no wonder later the login operation faster so much, it is no longer from the database to fetch data from you there, ah, hey, you're distributed. Have you ever been to another machine?

He said how could I only send a get to that machine through the network every time, put command to access the data only, the others do not know.

Like the last time I ran into a database connection thread when I was waiting, I knew it. He is also a connection pool, almost identical to our thread pool.

He said that some of the packages were so perverted that it was exhausting to look at the order data for a year.

I said pull it down you, you that is pure data, after you pass the data to me, I also have to assemble into HTML, the workload does not know how much bigger than you.

He said you have to work well with memecached, directly from him to take the data, as little as possible directly to call the database, our JDBC connection can also live easy point.

I said yes, well, the point is you have to get the data in advance to 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?

One day I met a terrible thing, almost died outside, not back to the thread pool ...

In fact, this distress I should be able to anticipate the right, too careless.

A few days ago I processed some deposits and withdrawals from HTTP, and the old thread 0x6900 me:

"Be very careful when handling these parcels, you must first obtain a lock, in the account deposit or withdrawal must be locked in the account, or else the thread will be waiting for you in the moment, sabotage, I was very coarse when I was young, and stabbed in the basket"

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

1, no lock-up situation

2, Lock the situation

I see the frightening, the original lock will bring such a serious accident.

From then on to see the deposit, the package of withdrawals is more careful, fortunately, there has been no accident.

Today I received a package is a transfer, from a famous actor's account to a famous director to make 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 account lock, in the famous director account lock.

But what I never thought of IS, there is a thread, yes, is 0x7954, unexpectedly at the same time from this director to the actor transfer.

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.

At this time I saw the thread 0x7954, he sat leisurely sitting there drinking coffee, I and he chatted up:

"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 am waiting for the lock, if I know which grandson does not release the lock, I will not beat him to death" 0x7954 not weakness.

I secretly took a look, this guy in his arms, holding me waiting for a director's lock?

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."

"No, I've never been able to release a lock until I've finished my work."

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 boss "operating system" also came.

After all, the Big Boss is well-informed, he glanced at, grunted, very disdain said:

"There's another deadlock."

"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 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, regardless of their knowledge.

Because not only the director and the actor, but also a lot of other people, boss left a table, inside is an algorithm 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 like the moral integrity of the house agent, beauty salons, like the waiter, standing at the door, like being played monkey loudly recite:

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

--------------------------------------------------------

After many days, I and other threads found a strange thing: the handling of parcels is getting easier

Regardless of any package, whether it's logging in, browsing, saving money ..... The steps are all the same, returning a fixed HTML page

Once I took a sneak peek, which said:

"The system will be maintenance upgrade from 00:00 to 4:00, we apologize for the inconvenience."

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 see things in the house are missing, waiting for the workshop, ready to shop, and even the CPU workshop has slowly disappeared.

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.

The next-generation thread pool will soon be reborn.

"Turn" a story to help you understand threads and thread pools

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.