The mysterious messenger went to the Java empire to preach the association, but was bombed out!

Mystery messenger

"It has long been heard that the Java language spans platforms and has many frameworks, but within 20 years of work, it has been promoted to the world's number one programming language. Seeing it today, it really deserves its reputation!"

"Mr. Messenger, you have passed the award. Let's go quickly. Your Majesty the King has been waiting for a long time."

Today, a mysterious visitor greeted the Java Empire.

The mysterious messenger went to the Java empire to preach the association, but was bombed

 

Coming to the main hall, I saw the king sitting in distress, closing his eyes to rest his mind, and not angering himself.

"Who is here?" asked the servant beside the king.

"I am the messenger of the GoLang Empire- Goroutine ", the messenger replied.

The mysterious messenger went to the Java empire to preach the association, but was bombed

 

"GoLang Empire? Where is a small country? The widow has never heard of it," the king said with his eyes closed.

After that, all the officials laughed.

"Why did you come here?" the waiter continued to ask.

The messenger replied: "I am here to preach."

After speaking, the king opened his eyes, "Preaching? My Java empire is the world's number one programming empire. Only we spread the word, how can we learn from others?"

The messenger was neither overbearing nor overbearing, saying: "Although the Java empire is in the midst of the sky, it has a flaw. Over time, it will become a major disaster."

"Oh, you are going to talk about it. If you can't make sense, you will blast out of the hall immediately." The king screamed.

"Dare to ask your majesty, what should the Java thread do when it reaches the blocking function?" the messenger asked.

Upon seeing this, the thread minister on the side stepped forward and said: "If it is blocked, it will naturally be suspended by the operating system and switch to another thread."

"Dare to ask adults, does thread switching cost? If a large number of threads switch frequently, what about the cost?" the messenger asked.

"If you are concerned about this issue, then you don't need to block the function and do it through asynchronous callbacks", the Minister of Threads replied.

The corner of the messenger's mouth raised and smiled slightly, "What an asynchronous callback! Asynchronous callback does not need to be blocked, but it has two sins. One is: the original code business logic is broken, and the other is that it is difficult to maintain in the hell of callback."

The mysterious messenger went to the Java empire to preach the association, but was bombed

 

"Neither does this, nor does that, you are really hard to serve," the Minister of Threads was a little anxious.

The messenger turned to the king and said, "Your Majesty, I have a way to allow threads to continue running without having to switch threads after encountering a blocking function, and without asynchronous callbacks. This is a high-concurrency development skill."

When the king heard this, he became interested: "Oh, there is such a thing? Come and listen."

The messenger paid a bow and said: "The thread can save the execution context after encountering the blocked place, and then execute the code elsewhere. After the blocked request is completed, it can then switch back to continue execution."

The mysterious messenger went to the Java empire to preach the association, but was bombed

 

The king was puzzled, and asked: "What does it mean to switch to executing code elsewhere? What does it mean to go back and continue execution? Can this function be executed halfway through and then exit and come back?"

"Yes, that's right!" the messenger replied.

As soon as the words came out, there was a lot of discussion in the court, and all the officials showed contemptuous smiles.

"It's ridiculous! The function execution is always done in one go from entering to returning and exiting. How can there be a half-way exit and then return to execute? It is unheard of!" said the thread minister on the side.

The messenger continued: "It's done in one go? I'm afraid not? In the middle of the thread execution function, when the time slice runs out or encounters I/O blocking, the operating system will hang after saving the context and switch to other threads. Then wait until the opportunity Go back and continue execution, right?"

The mysterious messenger went to the Java empire to preach the association, but was bombed

 

The Minister of Threads angrily scolded: "The strong word is unreasonable! What you said is that the operating system is scheduling and managing multiple threads, which are transparent to our application layer threads, so you don't need to care."

The messenger did not give in, but asked: " Since the operating system can schedule and manage multiple threads, why can't threads schedule the execution of management functions? "

The ministers whispered again and started talking.

"Your Majesty, these state envoys have deceived the crowd, and the Weichen suggested that they should be expelled from the hall immediately, so that they can watch and listen!"

The king agreed and immediately sent people forward.

Without waiting for the guards to step forward, the messenger left on his own and said as he walked: "Sigh! The dignified Java empire can't accommodate a new technology."

Farewell meeting

The messenger was discouraged and planned to leave the Java empire, but was stopped halfway through.

"Sir, please stay, my host invites your husband to meet at your house"

When the messenger came to the mansion, it turned out that the owner was a rich local squire.

"My husband is in the court today. I have heard that I am very interested in the interruption and recovery technology during the execution of the function mentioned by my husband. I would like to ask my husband for your advice."

"I don't dare to teach me. I came to the Java empire this time. The way I preach is called Xiecheng. It is a stunt of high concurrent development. But the monarch and ministers of your country are ignorant and return without success. What a pity, what a pity !", the messenger sighed.

The mysterious messenger went to the Java empire to preach the association, but was bombed

 

"Coroutine? What is this? I have only heard of processes and threads, but I have never heard of coroutines."

The messenger got up and said: "A thread is an execution flow abstracted by the operating system, which is uniformly scheduled and managed by the operating system. In a thread, multiple execution flows can also be abstracted, and the thread is used for unified scheduling and management. The abstraction above this thread The flow of execution is a coroutine"

The owner was a little puzzled, and asked: "How can a thread have multiple execution streams?"

"This is what I said at the court today. After the thread execution function encounters a blockage, it can save the context and exit, and then execute the code elsewhere. Here it is from one execution flow to another execution flow", the messenger explained .

The host took the case and started, "It turned out to be this, wonderful, wonderful! However, this thread is the operating system scheduling management, the execution flow abstracted out of the thread, that is, the coroutine, how to schedule management? Operation The system can deprive threads of execution rights through clock interrupts and system calls to enter the kernel. How can threads deprive the execution rights of coroutines to achieve scheduling management?"

The mysterious messenger went to the Java empire to preach the association, but was bombed

 

"Really a good question! The scheduling of threads is managed by the operating system, which is preemptive scheduling. Unlike coroutines, coroutines need to cooperate with each other and actively hand over execution rights. This is also the name of coroutines - the origin of collaborative programs . "

"Proactively hand over the right of execution? How to do it?" the owner asked.

"There are many ways. For example, the C++ empire has a coroutine framework called libco, which implements the intervention of the scheduler through HOOK key system functions."

"Then how do you Golang do it? Is that also true?"

"Our Golang empire is different. Our innate design is to support coroutines. System calls are encapsulated by us. Applications that need to be blocked when calling, such as file read and write Read/Write, Sleep, and our scheduler can I have a chance to intervene to perform scheduling management," said the messenger proudly.

The mysterious messenger went to the Java empire to preach the association, but was bombed

 

The host thought for a moment and asked: "Then how do we implement Java, please enlighten me, Mr."

"Your Java language is executed through the JVM, and the execution of the bytecode is under the control of the JVM. Is it not easy to interrupt and resume the execution flow of the application code?", the messenger said.

The master nodded, thoughtfully.

New journey

The host and the messenger talked happily, and it was almost dusk unknowingly.

The host stood up and said: "Mr. Jinmeng's enlightenment, I will comfort my life. Please stay in the house for more time, I will ask carefully."

The messenger waved his hand again and again and said: "I have something to do, and I will leave tomorrow."

"I don't know where the husband wants to go?"

"I heard that the C++ Empire will release a new version again, I plan to go to preach"

The owner was puzzled: "Isn't there libco in the C++ empire?"

"Libco is not a court thing after all. I am going now, hoping to include the coroutine into the new official standard"

The mysterious messenger went to the Java empire to preach the association, but was bombed

 

In the early morning of the next day, the messenger bid farewell to his master and rode away.

Soon, news came from the court of the Java Empire that some folks launched a coroutine framework- Quasar , and the government and the opposition shook for a while.

Guess you like

Origin blog.csdn.net/yelvgou9995/article/details/109096752