Multithreading (2) Thread, multithreading thread

Source: Internet
Author: User

Multithreading (2) Thread, multithreading thread

Let's start with the most basic Thread.

 

Create and start a thread

Create and start a thread with the following code:

1 namespace ConsoleApplication17 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 var thread = new Thread (PrintNumbers); 8 thread. start (); 9 10 Console. writeLine ("Thread Start... "); 11 Console. readKey (); 12} 13 14 // <summary> 15 // Method 16 that matches the delegate /// </summary> 17 public static void PrintNumbers () 18 {19 Console. writeLine ("Starting ...... "); 20 for (int I = 0; I <10; I ++) 21 {22 Console. writeLine (I); 23} 24} 25} 26}View Code

 

Running result:

 

Pause a thread

If you need to pause the current Thread, you can call the Thread. Sleep method to make the current Thread in a blocking state. The following code:

1 namespace ConsoleApplication17 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 var thread = new Thread (PrintNumbersWithDelay); 8 thread. start (); 9 10 Console. writeLine ("Thread Start... "); 11 Console. readKey (); 12} 13 14 /// <summary> 15 /// 16 /// </summary> 17 public static void PrintNumbersWithDelay () 18 {19 Console. writeLine ("Starting ...... "); 20 for (int I = 0; I <10; I ++) 21 {22 Thread. sleep (TimeSpan. fromMilliseconds (1000); 23 Console. writeLine (string. format ("{0} {1}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"), I); 24} 25} 26} 27}View Code

 

Output result:

Merge thread

If you need to wait for a sub-Thread to execute a row before the main Thread continues execution, you can use the Thread. Join method to implement the following code:

1 namespace ConsoleApplication17 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // Thread 8 var thread = new Thread (PrintNumbersWithDelay); 9 thread. start (); 10 thread. join (); 11 12 Console. writeLine ("Thread Completed! "); 13 Console. readKey (); 14} 15 16 /// <summary> 17 // 18 /// </summary> 19 public static void PrintNumbersWithDelay () 20 {21 Console. writeLine ("Starting ...... "); 22 for (int I = 0; I <10; I ++) 23 {24 Thread. sleep (TimeSpan. fromMilliseconds (1000); // The thread is blocked for 1 s, and the thread status is WaitSleepJoin25 Console. writeLine (string. format ("Current Time: {0}, thread status: {1}, result: {2}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"), Thread. currentThread. threadState, I); 26} 27} 28} 29}View Code

Output result:

Terminate thread

If you force terminate a subthread when it is running, you can call the Thread. Abort method. This will trigger a ThreadAbortException exception for the current subthread and cause the Thread to be terminated!

The following code:

1 namespace ConsoleApplication17 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 Console. writeLine ("Starting Program... "); 8 var thread = new Thread (PrintNumbersWithDelay); 9 thread. start (); 10 11 Thread. sleep (TimeSpan. fromMilliseconds (6000); 12 thread. abort (); 13 14 Console. writeLine ("Thread has been abort! "); 15 Console. readKey (); 16} 17 18 // <summary> 19 // 20 /// </summary> 21 public static void PrintNumbersWithDelay () 22 {23 Console. writeLine ("Starting ...... "); 24 for (int I = 0; I <10; I ++) 25 {26 Thread. sleep (TimeSpan. fromMilliseconds (1000); 27 Console. writeLine (string. format ("Current Time: {0}, thread status: {1}, result: {2}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"), Thread. currentThread. threadState, I); 28} 29} 30} 31}View Code thread passing Parameters

Through analysis, we can find that the Thread actually accepts a delegate, including a non-parameter delegate and an Object-type delegate,

 

The following code:

1 namespace ConsoleApplication17 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 Console. writeLine ("Main thread starting... "); 8 var thread = new Thread (PrintNumbersWithCount); 9 thread. start (5); 10 thread. join (); 11 12 Console. writeLine ("Main thread completed! "); 13 Console. readKey (); 14} 15 16 /// <summary> 17 // match the delegate method, 18 /// </summary> 19 public static void PrintNumbersWithCount (object obj) 20 {21 Console. writeLine ("Sub thread starting... "); 22 var number = Convert. toInt32 (obj); 23 for (int I = 0; I <number; I ++) 24 {25 Console. writeLine (string. format ("Current Time: {0}, thread status: {1}, result: {2}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"), Thread. currentThread. threadState, I); 26} 27} 28} 29}View Code

 

Output result:

 

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.