I’m a thread

the autobiography of a thread：birth,death and rebirth

Photo by Justin Lim on Unsplash

1.A newbie

I am a thread. I was born with a number 0x3704, and then led to a dark room where I found a lot of friends just like me.

My companion 0x6900 stayed here for a rather long time. He said to me in a sophisticated tone: “ The destiny of us thread is to deal with packages. You have to come back here as soon as you finish handling the package, or you may never come back.”

I asked innocently: “ Package ? What package? ”

“ Don’t worry, you’ll soon find out. We don’t raise idle ones here.”

It wasn’t long before the door opened. A ferocious guy yelled, “ 0x3704, come out! ”

A heavy package was pressed on me. There was also a paper listing operation steps.

“Go, deal with this package.”

“But where to? ”

“ Follow the instructions. First to the Runnable workshop.”

I found an arrow on the ground. It led me to a bright house, which already accommodated some threads. Everyone seemed nervous, ready to dash forward at any moment.

As soon as I came in, I heard the radio saying, “0x3704, enter the Running workshop.”

I walked faster. Someone was talking behind me.

“Such a luck dog! He can run right after being runnable! ”

“ Maybe he knows SOMEONE.”

“No. He has higher priority. Alas!”

Running workshop was right in front of me. It was so beautiful. No wonder the old thread kept nagging about how he wanted to stay here forever.

The room was bigger and had nicer views. The air was fresh and fragrant. There were many workers waiting to serve me.

They all had numbers. More importantly, they had tags saying “hard disk”, “database”, “memory”, “network card” and so on.

Just then I did not know about their job. Let’s check the operation steps.

Step one: Unpack the parameter.

There was a HttpRequest object inside, including two parameters: userName and password.

Step two: Perform login operation.

Well, someone was trying to login. I handed userName and password to the database waiter. He left slowly.

But why was he so slow? Anyway, I could stay here longer since the third step is not operable.

Just at this moment, the broadcast said “0x3704, this is CPU. Remember the step you are taking, and then leave with the package right away!”

I slowly began to clean up my things.

“Hurry up, the other thread is about to enter.”

I left this workshop and entered another big room, where many threads were drinking tea and playing cards with laziness.

“ Dude, you have nothing to do now?”

“You must be new here. I am waiting for the database waiter to give me my data. It is said that they are hundreds of thousands of times slower than us. Let’s have a good rest here.”

“How slow they are! I have someone here waiting to login, can they wait that long?”

“ Relax, don’t you know that a day in reality is equal to a year in CPU? Our time is counted by nanosecond and millisecond. As a result, a second in reality is equal to a couple of days in our world. We can definitely make it.”

Maybe I could get some sleep. Some time later, the loudspeaker started to work again: “0x3704, your data is coming, go and execute it!”

I turned and ran to the CPU workshop, only to find that it is a one-way door. I cannot get into the workshop through this door.

There was a roar of laughter from behind. “He is a newbie for sure. He doesn’t even know that we need to wait in the Runnable workshop!”

I hurried to the Runnable workshop. Being unlucky this time, I waited a long time before entering the CPU workshop.

While I was waiting, I heard people whispering.

“You know what, a thread was killed recently.”

“But why? ”

“That guy refused to leave the CPU workshop, causing the CPU utilization to remain 100%. So he was killed.”

“Where was he then? ”

“Maybe he was collected as a garbage.”

I secretly gave a shudder and worked faster. The second step was done.

Step three: Build the home page after successful login

This step was a bit time-consuming because there were a lot of HTML to deal with. Whoever wrote the code made it an annoying job for me to process.

While I was busy operating the HTML, CPU began to yell again.

“0x3704, this is CPU. Remember the steps you are taking, and then leave with the package right away!”

“Why should I? ”

“Each thread can only run on CPU for a while, and then it will be others’ turn. So go to the Runnable workshop and wait for your turn. ”

Then I have been switching among “runnable”, “waiting” and “running” for many times, and finally completed the work following the step list.

Finally, the package containing HTML was successfully sent back. As for what’s going on after login, I don’t care. It’s about to get back to my dark room. I felt a little unwilling.

But compared with some other threads, I am lucky enough. They are completely destroyed after operation, and I am still alive!

Back to the little black house, I was asked by the old thread 0x6900.

“How’s your first day?”

“The rules in our world are complex. First of all, you don’t know when is your turn; secondly, you may be interrupted at any time in the course of execution and asked to leave the CPU workshop; thirdly, once there are a time-consuming operation involving hard disk or database, you need to leave the CPU and wait; and fourthly, the chances are that you cannot start to work right away even if you have the data . You’ll have to wait for the CPU to pick.”

“Well enough, dude.”

“I just don’t understand why some threads died right after they complete their work, while we are still alive.”

“You know what, eternity is our prerogative. And we have an official name for our place: thread pool.”