Multi-thread coding in. NET-3-Thread.Sleep

Source: Internet
Author: User

  Reading directory

  1. What is Thread. Sleep?

Ii. Example

Iii. when to use a thread?

Iv. disadvantages of using threads

  1. What is Thread. Sleep?

1. Thread. Sleep () itself means to suspend the current Thread for a specified time.

2. MSDN Description: Specify zero (0) to indicate that this thread should be suspended so that other waiting threads can be executed.

Thread. sleep (0) does not mean that the Thread needs to be suspended for 0 milliseconds. it is meaningless to call Thread this time. the current thread of Sleep (0) has been frozen for a moment, giving other threads the opportunity to prioritize execution. Thread. Sleep (0) indicates that your Thread temporarily abandons the CPU, that is, releasing unused time slices for other threads or processes, which is equivalent to a give-up action. We can understand that it is actually to suspend the current thread so that other threads can seize CPU resources again with the current thread.

3. Because the CPU resources are limited, multiple threads in the process must seize the CPU, which also leads to multiple threads in the process to execute alternately.

For example
People are queuing for shopping.
It's your turn, and you may have no idea what to buy or what to consider. In short, you don't have to worry about buying.
So you can give way, You can Thread. Sleep (100)
Let the 100 people in the queue behind you buy it again.
Thread. Sleep (0)
Give up
Someone is on the back, and you are on the back.
If no one is behind you, you can naturally be placed in your original position.

 

 

Ii. Example

View Code

 1 delegate void OutPutDelegate(); 2 static void Main(string[] args) 3 { 4       Console.WriteLine("Now main thread begin"); 5       OutPutDelegate outPutDelegate = new OutPutDelegate(OutPutData); 6       Thread thread = new Thread(new ThreadStart(outPutDelegate)); 7        thread.Start(); 8        for (int i = 0; i < 5; i++) 9        {10             Console.WriteLine(i);11             Thread.Sleep(0);12         }13         Console.ReadKey();14 }15 16 private static void OutPutData()17 {18         Console.WriteLine("Orther Thread");19          for (int i = 0; i < 10; i++)20          {21               Console.WriteLine(i);22          }23          Console.ReadKey();24 }

The main Thread hangs up and resources are re-allocated. The main Thread and the Orther Thread seize CPU resources again. Orther Thread did not seize CPU resources, and finally the main Thread grabbed CPU resources, therefore, 0-4 is output first.

When the main Thread hangs up and resources are re-allocated, the main Thread and the Orther Thread seize CPU resources again, and the main Thread grabs CPU resources, so the number 1 is output, orther Thread didn't grab the CPU resources at the beginning, and later Orther Thread grabbed the CPU resources and output the number 0-9. multiple threads run alternately.

The main Thread hangs up and resources are re-allocated. The main Thread and the Orther Thread seize the CPU resources again. The main Thread did not grab the CPU resources, and the Orther Thread grabbed the CPU resources and output the numbers 0-9 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.