First of all, in the WINDOWNT environment, Linux is a completely different effect, depending on the CPU resource allocation algorithm of the operating system.
Background introduction:
If there are three (or more) threads executing their own algorithm asynchronously (not knowing which SB writes the algorithm, rather consuming resources), then how is the CPU resources of the three threads allocated?
The first piece of code and
1 class Program2 {3 Static voidMain (string[] args)4 {5 inti =0;6 while(i< -)7 { 8 NewThread (NewParameterizedthreadstart (o =9 {TenConsole.WriteLine ("*************************"); Onei++; A })). Start (); - NewThread (NewParameterizedthreadstart (o = - { theConsole.WriteLine ("++++++++++++++++++++++++"); -i++; - })). Start (); - NewThread (NewParameterizedthreadstart (o = + { - while(I < -) + { A Console.WriteLine (i.ToString ()); ati++; -Thread.Sleep (0); - } - })). Start (); - } - Console.read (); in - } to}
If the 24th line of comments, the results are very different, then Threadsleep (0) The role of the obvious, although from the code logically understand that it should be a thread sleep 0 milliseconds, want to do more than anything, for the Windows operating system is very different, this sentence meaning system understood as This time does not participate in CPU resource competition.
Because the CPU resource allocation of window is allocated in the way of "democracy", all the threads that need resources are queued in the system, the system will give them a rating, which one needs resources most, which one least needs resources, which is equivalent to adding a weight to each resource applicant (if the applicant is a beauty, Weight is naturally higher). When the previous user freed the CPU, the system will arrange the next to use the CPU (said so much, did not see what the difference with Linux, but the following is not the same), when the user consumes the CPU, must occupy the active release, otherwise the CPU will always be occupied.
Therefore, when doing multithreading should pay attention to the release of resources.
If there is a mistake, look correct!
The principle of multi-threading and CPU Resource allocation in window environment