我们先从最基础的Thread说起。

 

创建并启动线程

创建并启动一个线程,如下代码:

 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方法,使当前线程处于阻塞状态,如下代码:

 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

 

输出结果:

合并线程

如果需要等待某个子线程执行行,主线程才继续执行时,可以使用Thread.Join方法来实现,如下代码:

 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

 输出结果:

终止线程

 如果在子线程运行过程中强制终止它,可以调用Thread.Abort方法,这会给当前子线程触发ThreadAbortException异常,导致线程被终止!

如下代码:

 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类型的委托,

 

如下代码:

 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

 

输出结果:

 

posted on 2017-06-20 17:05  永远的麦子  阅读(666)  评论(0编辑  收藏  举报