随笔 - 410  文章 - 0  评论 - 519  阅读 - 148万 

  我们先从最基础的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   永远的麦子  阅读(668)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示