Joke Collection Website - Mood Talk - What are the differences and connections between C# asynchronous and multi-threading?
What are the differences and connections between C# asynchronous and multi-threading?
There is no necessary connection between asynchronous and multi-threading. It can only be said that asynchronous can be realized through multi-threading.
To understand these things, you must have a lot of relevant knowledge, operating systems Principles, compilation principles, etc.
To put it simply, the computer or CPU executes your code sequentially. The current statement has not been executed, and subsequent statements will not be executed. This is never possible. Changed rules!
However, such a mechanism will make the entire execution efficiency very low, because the CPU always has to wait for the current statement to be executed before executing the following code, and the computer is waiting for the user most of the time. input, waiting for the disk file reading to be completed, and waiting for the network data transmission to be completed. Therefore, in order to improve the operating efficiency of the entire system and improve the execution efficiency of the CPU, the computer system has introduced an asynchronous mechanism.
The asynchronous mechanism, simply put, means that the CPU will not wait stupidly, it will do other things first, and when what it needs has been prepared (in most cases, it will wait for external The input data has been prepared, and some complex calculations have been completed), the hardware system gives the CPU an interrupt, telling the CPU that there is new data, and you can process it now.
This is the most basic asynchronous principle, and modern operating systems have well encapsulated the hardware system, shielded many underlying mechanisms, and provided program developers with the concepts of events and multi-threading. Allow program developers to make better use of asynchronous mechanisms.
Let’s talk about multi-threading. Multi-threading is a concept that only came into existence after the computer introduced the operating system. As for why the computer introduced the operating system, I will not describe it here. You can understand it yourself. There are two methods to implement multi-threading. One is to increase the number of CPU cores and number. This is the most direct method. The other is to use CPU polling. Each process executes for a period of time and then executes another. Since the switching speed is very fast, the instructions of a process give the impression that they are executed at the same time.
Having said so much, you may be disappointed because there is no code or examples. But what I want to say is that this problem cannot be explained with code. Different operating systems, different programming languages, and different programming frameworks have different definitions and processing of asynchronous and multi-threading, but the principle is like this.
When we write code, we just write a bunch of instructions to ask the CPU to execute. The synchronization method is like this:
Me: Hi, CPU, I want you to do something. And it must be done immediately, because I am waiting for you.
CPU: Just wait, I will do it now.
The asynchronous method is like this:
Me: Hi, CPU, I want you to do something, but not right away. You can leave it to the people below. , or find a helper, and we will continue to do the next thing.
CPU: Okay, I will tell the hardware to do it now, and I will tell you through the event when the results are available
Or
CPU: Okay, I'll let another core do this and let's continue (in fact, multi-threading is implemented by the operating system, I'm just giving this as an example)
- Related articles
- How to learn African tambourine from scratch?
- Classic sayings about expressing shortcomings (35 selected sentences)
- Talking about beautiful ancient styles
- About Lantern Festival, short sentences, funny Lantern Festival sentences
- Good night quotes for friends circle: The end of the road is still a road, as long as you are willing to walk
- How many days are suitable for traveling in Enshi?
- What is essential oil for (what is essential oil? )
- How to write the outlook for 2022?
- Talking about sentences suitable for friends on rainy days; On Sentences Suitable for Rainy Friends (65 Selected Sentences)
- Talk about the sadness of losing loved ones.