C # multi-thread, asynchronous, lock, comprehensive resolution (1): first, say hello to the thread,

Source: Internet
Author: User

C # multi-thread, asynchronous, lock, comprehensive resolution (1): first, say hello to the thread,

Welcome to the world of multithreading. Here we have a boss called CPU. That's right, CPU manages all the threads. Every thread is waiting to be flattered by the CPU in the harem. It should be the most valuable time in a thread's life.

 

Well, before learning multithreading, it is necessary to master the use and principle of delegation. If you are not clear about delegation, you can refer to the two previous articles, in the face of Delegation for easy-to-understand explanations.

 

We know that many Members can be defined in a class. For example, the frequently used method is one of the members. The following code:

The above method name is Coding. With the name of this method, we can call this method multiple times in the program or assign this method to the delegate.

For example, if the above method is assigned to a delegate, you can write it like this:

In fact, I have defined a method that is only intended to be used once. Therefore, we do not need to define a method separately, and then assign that method to a delegate. Therefore, I plan to write the above Code directly into a line, just as we usually write code like this:

Therefore, I plan to write the value assignment of the delegate as a row on the right, as shown below:

In this way, you don't need to define the Coding method separately. After all, I only plan to use the Coding method once. However, what is the error? Note that C # does not support this syntax. However, it doesn't matter. At least you can think this way to explain that your ideas are correct. We know that before calling a method, we must specify the input and output data types of the method. For a method that is only intended to be used once, the private modifier is not very important, so we can further modify the above Code and remove private:

The error is still reported, indicating that the syntax is not correct. In fact, the return type string of the Coding method does not have much significance. Note that in the Coding method body, a string (string type) is returned after return ), C # itself has the type inference function. If the return value is followed by a string, it indicates that this method must return a string type. Therefore, the string type is also removed because this method is only used once, the method name is meaningless and can be removed. The method name is called multiple times, but we only want to use it once, so the method name is also removed, the final result is as follows:

The above code still reports an error. What's the problem? Does it surpass the C # syntax? Yes, it does go beyond the C # syntax. C # is written as follows:

Oh? Are the methods input separated from the method bodies? Yes, it's very C #. The preceding method does not have a method name. We call it an anonymous method.

In fact, the above Code can be further abbreviated, because C # has the type inference function. An expression is valid because the data type on the left and right of the expression is the same, as shown below:

Int n = 9;

The expression is int on the left and 9 on the right. In turn, because the right side of the expression is 9, the data type on the left must be accepted for data types such as 9. Therefore, the int Declaration on the left side of the expression is OK. This is the type inference function of C.

The preceding delegated value assignment expression can also be abbreviated as follows:

The two writing methods above can be used. In fact, there is only one method parameter, which can also be omitted (), as shown below:

In the end, there was such a convenient way of writing, which is a short form from an anonymous method. We call it Lambda expressions (supported since C #3.0 ).

Well, we should go back to the thread topic.

 

Why is multithreading required?

The reason is simple. In some cases, some functions cannot be completed without multithreading. For example, it may take a long time to query a database in WinForm, we usually pop up a Form with the Loading image covered, prompting the user to wait. At this time, such a function cannot be completed without multithreading. After all, a single thread can only process one task and then process another task.

As the boss CPU in the thread world, it has a feature and an important feature: Windows systems stipulate that the CPU can only run one thread at a time, this thread will be flattered by the CPU for about 30 milliseconds, and the CPU will switch to another thread. the discarded thread will temporarily enter the harem and wait for it to be again flattered. Therefore, the CPU will be lucky for many threads in one second. The CPU here refers to a single-core CPU. That is to say, if the CPU has multiple kernels, multiple threads can be run at the same time. For example, Intel i7's 4-core CPU can run four threads at the same time. If it is dual-core, two threads can be run at the same time.

 

For example, QQ is used in many threads, but it does not mean that QQ is always running. The CPU will run QQ within 30 milliseconds, and then thread switching will be started immediately, QQ is in the waiting state. At this time, it may switch to the 360 software. After 360 is run for 30 milliseconds, the CPU starts switching to another program. The CPU is a repetitive worker until the functional commands issued by all programs opened on the user's computer are processed.

As to the rules for CPU switching to the next thread, the algorithm depends on the CPU scheduling of windows.

 

For windows, every program in the computer, such as QQ, notepad, word, PS, VS, 360, and IE, has a common name: process.

Like our human beings, there are people such as Zhang San, Li Si, and Wang Wu. But for countries, they also have a common name: citizens.

 

So when I open a QQ, a process is instantiated in Windows. Each process has a thread by default: the main thread. Of course, more threads can be used in this program.

 

I will go here today and give a further introduction later.

If you have more discussions about C # technology, you can add one of my QQ group: 240749438.

 

Related Article

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.