多線程學習(一)

線程的基本操做
線程的基本操做包括:建立線程、暫停線程、線程等待、終止線程。git

 1 /*-----------------------------------------------------------------------
 2                         written by helio, 2019
 3                             ThreadSample1
 4 -----------------------------------------------------------------------*/
 5 using System;
 6 using System.Threading;
 7 
 8 namespace ThreadSample
 9 {
10     class Program
11     {
12         static void FirstPrintNumbsers()
13         {
14             Console.WriteLine("{0} starting..", Thread.CurrentThread.Name);
15             for (int i = 1; i <= 10; i++)
16             {
17                 Console.WriteLine(i);
18                 Thread.Sleep(TimeSpan.FromMilliseconds(100));
19             }
20         }
21 
22         static void SecondPrintNumbers()
23         {
24             Console.WriteLine("{0} starting...", Thread.CurrentThread.Name);
25             for (int i = 1; i <= 10; i++)
26             {
27                 Console.WriteLine(i);
28                 Thread.Sleep(TimeSpan.FromMilliseconds(100));
29             }
30         }
31         static void Main(string[] args)
32         {
33             Thread t1 = new Thread(FirstPrintNumbsers);
34             Thread t2 = new Thread(SecondPrintNumbers);
35             t1.Name = "Thread1";
36             t2.Name = "Thread2";
37             t1.Start();
38             t2.Start();
39             t1.Join();                    
40             t2.Join();
41 
42             Thread.CurrentThread.Name = "MainThread";
43             FirstPrintNumbsers();
44             Console.ReadKey();
45         }
46     }
47 }

  工做原理
在Main方法外定義了方法FristPrintNumbers、SecondPrintNumbers,該方法會被主程序和向建立的兩個線程Thread一、Thread2使用。建立完成線程後,使用Start方法啓動線程,使用Join方法值線程執行完成後繼續執行主線程或者該線程下面的線程。github

前臺線程和後臺線程spa

 1 ```
 2 /*-----------------------------------------------------------------------
 3                         written by helio, 2019
 4                             ThreadSample2
 5 -----------------------------------------------------------------------*/
 6 using System;
 7 using System.Threading;
 8 
 9 namespace ThreadSample
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             var sampleForeground = new ThreadSample(10);
16             var sampleBackground = new ThreadSample(20);
17 
18             var ThreadOne = new Thread(sampleForeground.CountNumbers);
19             ThreadOne.Name = "ForegourndThread";
20             var ThreadTwo = new Thread(sampleBackground.CountNumbers);
21             ThreadTwo.Name = "BackgroundThread";
22             ThreadTwo.IsBackground = true;
23 
24             ThreadOne.Start();
25             ThreadTwo.Start();
26         }
27 
28         class ThreadSample
29         {
30             private readonly int m_iteration;
31 
32             public ThreadSample(int iteration)
33             {
34                 m_iteration = iteration;
35             }
36 
37             public void CountNumbers()
38             {
39                 for (int i = 0; i < m_iteration; i++)
40                 {
41                     Thread.Sleep(TimeSpan.FromSeconds(0.5));
42                     Console.WriteLine("{0} prints {1}",
43                         Thread.CurrentThread.Name, i);
44                 }
45             }
46         }
47     }
48 }
49 ```

  工做原理線程

  當主程序啓動時定義了兩個不一樣的線程。默認狀況下,顯示建立的線程是前臺線程。經過手動設置ThradTow對象的IsBackkgournd來建立一個後臺線程。<br/>
  當一個程序中的前臺線程完成工做後,程序就會關閉,即便還有未完成工做的後臺線程。因此在上述程序中,前臺線程Thread1完成工做後程序就會別關閉。code

閱讀原文可訪問個人我的博客對象

相關文章
相關標籤/搜索