多線程(2)Thread

  咱們先從最基礎的Thread提及。ide

 

建立並啓動線程

建立並啓動一個線程,以下代碼:spa

 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         /// 匹配委託的方法
16         /// </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

 

運行結果:線程

 

暫停線程

假如須要暫停當前線程,能夠調用Thread.Sleep方法,使當前線程處於阻塞狀態,以下代碼:3d

 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

 

輸出結果:code

合併線程

若是須要等待某個子線程執行行,主線程才繼續執行時,能夠使用Thread.Join方法來實現,以下代碼:orm

 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));//線程阻塞1s,此時線程狀態爲WaitSleepJoin
25                 Console.WriteLine(string.Format("當前時間:{0},線程狀態:{1},結果:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),Thread.CurrentThread.ThreadState, i));
26             }
27         }
28     }
29 }
View Code

 輸出結果:blog

終止線程

 若是在子線程運行過程當中強制終止它,能夠調用Thread.Abort方法,這會給當前子線程觸發ThreadAbortException異常,致使線程被終止!string

以下代碼:it

 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("當前時間:{0},線程狀態:{1},結果:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ThreadState, i));
28             }
29         }
30     }
31 }
View Code

線程傳遞參數

經過分析能夠發現,Thread接受的其實是一個委託,包括無參數的委託和接受一個Object類型的委託,io

 

以下代碼:

 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         /// 匹配委託方法,帶參數
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("當前時間:{0},線程狀態:{1},結果:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ThreadState, i));
26             }
27         }
28     }
29 }
View Code

 

輸出結果:

相關文章
相關標籤/搜索