C # Multi-threading, asynchronous, lock, full resolution (1): Let's say hello to the thread first.

Source: Internet
Author: User

Welcome to the world of multithreading, where there is a boss, it is called CPU, yes, the CPU manages all the threads. Every thread in the harem waiting to be the CPU, the favor should be the most valuable thread life time it.

Well, before learning multithreading is necessary to master the use and principle of the Commission, if the Commission is not clear, you can see my previous 2 articles, inside the delegation of the easy to understand the explanation.

We know that many members can be defined in a class, for example, a method that is used frequently is one of the members, as in the following code:

The method name above is: Coding, just 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, assigning the above method to a delegate can be written like this:

In fact a lot of times, I define a only plan to use this method once. So there's no need to define a method individually and then assign that method to a delegate, so I'm going to write the code directly into a line, just like we normally do:

So, I'm going to write the assignment of the delegate to the right line, as follows:

This way, you do not have to define coding this method, after all, coding method, I only intend to use once. But, however, the error? Note that C # does not support this syntax, but it doesn't matter, at least you can think of it, stating that your thinking is correct. We know that before invoking a method, it is important to explicitly define the input and output data types of the method, and for a method that is only intended to be used once, the private modifier is not significant, so the above code is further modified to remove the private:

Still error, the syntax is not correct, in fact, the return type of the coding method string does not have much meaning, note that the coding method body returns after Return is a string (string type), C # itself has type inference function, Return is followed by a string, that means that this method must return a string type, so the string is also removed, because this method is used only once, then the name of the method is meaningless, can also be removed, the name of the method is used for multiple calls, And we are only going to use it once, so the method name is also removed, and finally it becomes this:

The above code also reported wrong, what ghost? Does it go beyond C # syntax? Yes, it does go beyond C # syntax. The correct wording for C # is this:

Oh? is the input of the method separated from the method body by = =? Yes, this is C #. The above notation does not have a method name, which we call the anonymous method.

In fact, the above code can be further shortened, because C # has type inference functionality. An expression is valid because the left side of the expression is identical to the data type on the right, as follows:

int n=9;

The left side of the expression is int, and the right is 9, so it's set. Conversely, because the right side of the expression is 9, the data type on the left must be acceptable for 9 such data types, so the left side of the expression is OK with an int declaration. This is the type inference feature of C #.

The above delegate assignment expression can also be simply as follows:

The above 2 kinds of writing can be, in fact, for the method parameter only one, you can also omit (), as follows:

In the end, this is a handy way to do this, which is a shorthand form of an anonymous method, which we call the lambda expression (supported from c#3.0).

Well, we should go back to the thread.

Why use multithreading?

The reason is very simple ah, sometimes do not use multithreading is unable to complete certain functions, such as: In the WinForm, query the database, it may take a long time to query, then in the query, we will generally pop up a loading image obscured form prompts the user to wait. This time if not multi-threaded words can not complete this function, after all, single-threaded only to deal with the completion of one thing to deal with another thing.

As the boss of the thread world CPU, it has a feature, very important features: the Windows system stipulates that the CPU can only run a thread at the same time, the thread will be the CPU for about 30 milliseconds, the CPU will switch another thread, The discarded thread will temporarily break into the harem and wait to be re-credited. So the CPU in 1 seconds will favor a lot of threads, think is happiness. The CPU referred to here is a single-core CPU. That is, if the CPU has multiple cores, it can run multiple threads at the same time. For example: Intel i7 's 4-core CPU can run 4 threads at the same time. If it is a dual-core, then you can run 2 threads at the same time.

For example, we use a lot of threads in the QQ,QQ, but not that QQ has been in operation, the CPU will run in 30 milliseconds QQ, then immediately after the thread switch, QQ is waiting, this time may switch to 360 software, then 360 will be run 30 milliseconds later, The CPU has started switching to another program again. The CPU is the worker who has been so repetitive until the function instructions issued by all the programs opened on the user's computer are processed to idle and rest.

As for what rules the CPU is following to switch the next thread to perform the algorithm that depends on the CPU scheduling of Windows.

For Windows, every computer program, such as: QQ, Notepad, Word, PS, VS, 360, ie, etc., they have a common name: process.

Just like us humans, someone called Zhang San, John Doe, Harry, etc., but for the country, they also have a generic name: citizens.

So when I open a QQ, is to instantiate a process in Windows, each process is the default one thread: Main thread, of course, the program can also use more threads.

Come here today, follow slowly in-depth introduction.

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

C # Multi-threading, asynchronous, lock, full resolution (1): Let's say hello to the thread first.

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.