.NET中线程的生命周期

开发 后端
我们今天要谈谈.NET中线程的生命周期。当我们创建一个线程后,我们需要调用线程对象的Start()方法来调度那个线程。

  当一个线程计划执行时它可以经过几个状态,包括未开始,活跃,睡眠,等等。线程类包含几个允许你启动、停止、恢复、退出、暂停以及等待一个线程的方法。我们可以使用ThreadState属性来获取线程的当前状态,状态值可能是ThreadState枚举中的一个:

  Aborted - 线程当前处理停止状态,但是不一定已经执行完。

  AbortRequested – 已经调用Abort() 方法但是线程还没有接收到将试图终止线程的System.Threading.ThreadAbortexception。虽然线程还没有停止,但是马上就会。

  Background - 线程在后台执行。

  Running - 线程已经启动而且没有被阻塞。

  Stopped - 线程已经完成了所有指令并停止了。

  StopRequested - 请求线程停止。

  Suspend - 线程已经被挂起。

  SuspendRequested - 请求挂起线程。

  Unstarted - 线程还没有调用Start()方法之前。

  WaitSleepJoin - 调用Wait(), Sleep() 或 Join() 方法后处理阻塞状态的线程。

  图3 显示了一个线程的生命周期。

图3

  在这部分,我们将讨论线程的生命周期

  让一个线程进入睡眠状态

  当我们创建一个线程后,我们需要调用线程对象的Start()方法来调度那个线程。在这时,CLR将会为作为构造函数参数传递给线程对象的方法地址分配一个时间片。一旦线程开始执行,它就可以在操作系统处理其他线程时回到睡眠状态或者退出状态。我们可以使用线程类的Sleep()方法让一个线程进入睡眠状态。如果你正在等待一个资源并且你想在稍后继续尝试访问这个资源时,Sleep()方法是很重要的。举个例子,假设你的程序由于无法访问需要的资源而导致其不能继续执行时,你可能想要在几毫秒之后尝试继续访问资源,在这种情况下让线程在再次尝试访问资源之前睡眠一段时间是一个很好的方式。

  Sleep()方法有两种重载方式。第一种重载方法有一个整型参数,并会按照指定的毫秒时间暂停线程执行。例如,如果你向线程传递值100,那么线程将会暂停100毫秒。这个方法将会让线程进入WaitSleepJoin状态。让我们看一个例子,thread_sleep2.cs:

  1. /*************************************  
  2.   /* Copyright (c) 2012 Daniel Dong  
  3.   *  
  4.   * Author:Daniel Dong  
  5.   * Blog: www.cnblogs.com/danielWise  
  6.   * Email: guofoo@163.com  
  7.   *  
  8.   */ 
  9.   using System;  
  10.   using System.Collections.Generic;  
  11.   using System.Text;  
  12.   using System.Threading;  
  13.   namespace SimpleThread  
  14.   {  
  15.   public class ThreadSleep  
  16.   {  
  17.   public static Thread worker;  
  18.   public static Thread worker2;  
  19.   public static void Main()  
  20.   {  
  21.   Console.WriteLine("Entering the void Main!");  
  22.   worker = new Thread(new ThreadStart(Counter));  
  23.   worker2 = new Thread(new ThreadStart(Counter2));  
  24.   //Make the worker2 Object as highest priority  
  25.   worker2.Priority = ThreadPriority.Highest;  
  26.   worker.Start();  
  27. worker2.Start();  
  28.   Console.WriteLine("Exiting the void Main!");  
  29.   Console.ReadLine();  
  30.   }  
  31.  public static void Counter()  
  32.   {  
  33.   Console.WriteLine("Entering Counter");  
  34.   for (int i = 1; i <50; i++)  
  35.   {  
  36.   Console.Write(i + " ");  
  37.   if (i == 10)  
  38.   {  
  39.   Console.WriteLine();  
  40.   Thread.Sleep(1000);  
  41.   }  
  42.   }  
  43.   Console.WriteLine("Exiting Counter");  
  44.   }  
  45.   public static void Counter2()  
  46.   {  
  47.   Console.WriteLine("Entering Counter2");  
  48.   for (int i = 51; i <100; i++)  
  49.   {  
  50.   Console.Write(i + " ");  
  51.   if (i == 70)  
  52.   {  
  53.   Console.WriteLine();  
  54.   Thread.Sleep(5000);  
  55.  }  
  56.   }  
  57.   Console.WriteLine("Exiting Counter2");  
  58.   }  
  59.   }  
  60.   } 

 

  Counter()方法从1到50计数,当到达10以后睡眠1000毫秒。Counter2()方法从51~100计数,当到达70时睡眠5000毫秒。下面是输出结果:

注:以上是在多核CPU下运行的结果,单核CPU 执行情况可能与上图有所出入。

  第二种重载方法有一个TimeSpan类型参数,当前线程会按照TimeSpan的值暂停一段时间。TimeSpan是System命名空间中的一个类。TimeSpan有一些很有用的属性并会返回基于时钟时间间隔。

  我们可以使用FromSeconds()和FromMinutes()来确定睡眠时间。下面是一个例子,thread_sleep3.cs:

 

  1.   public static void Counter()  
  2.   {  
  3.   Console.WriteLine("Entering Counter");  
  4.   for (int i = 1; i <50; i++)  
  5.   {  
  6.   Console.Write(i + " ");  
  7.   if (i == 10)  
  8.   {  
  9.   Console.WriteLine();  
  10.   Thread.Sleep(TimeSpan.FromSeconds(1));  
  11.  }  
  12.   }  
  13.   Console.WriteLine("Exiting Counter");  
  14.   }  
  15.   public static void Counter2()  
  16.   {  
  17.   Console.WriteLine("Entering Counter2");  
  18.   for (int i = 51; i <100; i++)  
  19.   {  
  20.  Console.Write(i + " ");  
  21.   if (i == 70)  
  22.   {  
  23.   Console.WriteLine();  
  24.   Thread.Sleep(TimeSpan.FromSeconds(5));  
  25.   }  
  26.   }  
  27.   Console.WriteLine("Exiting Counter2");  
  28.   } 

 

  输出结果与thread_sleep2类似。

  中断一个线程

  当让一个线程睡眠时,它实际会进入WaitSleepJoin状态。如果线程处理睡眠状态,那么在它超时退出之前唯一可以唤醒线程的方式是使用Interrupt()方法。Interrupt()方法将让线程回到调度队列中去。让我们看一个例子,thread_interrupt.cs:

 

  1.   /*************************************  
  2.   /* Copyright (c) 2012 Daniel Dong  
  3.   *  
  4.   * Author:Daniel Dong  
  5.   * Blog: www.cnblogs.com/danielWise  
  6.   * Email: guofoo@163.com  
  7.   *  
  8.   */ 
  9.   using System;  
  10.   using System.Collections.Generic;  
  11.   using System.Text;  
  12.   using System.Threading;  
  13.  namespace SimpleThread  
  14.   {  
  15.   public class Interrupt  
  16.   {  
  17.   public static Thread sleeper;  
  18.   public static Thread worker;  
  19.   public static void Main()  
  20.   {  
  21.   Console.WriteLine("Entering the void Main!");  
  22.   sleeper = new Thread(new ThreadStart(SleepingThread));  
  23.   worker = new Thread(new ThreadStart(AwakeThread));  
  24.   sleeper.Start();  
  25.   worker.Start();  
  26.   Console.WriteLine("Exiting the void Main!");  
  27.   Console.ReadLine();  
  28.   }  
  29.  public static void SleepingThread()  
  30.   {  
  31.   for (int i = 1; i <50; i++)  
  32.   {  
  33.   Console.Write(i + " ");  
  34.   if (i == 10 || i == 20 || i == 30)  
  35.   {  
  36.   Console.WriteLine("Going to sleep at: " + i);  
  37.   try 
  38.   {  
  39.   Thread.Sleep(20);  
  40.   }  
  41.   catch (ThreadInterruptedException ex)  
  42.   {  
  43.   Console.WriteLine(ex.Message);  
  44.   }  
  45.  }  
  46.   }  
  47.   }  
  48.   public static void AwakeThread()  
  49.   {  
  50.   for (int i = 51; i <100; i++)  
  51.   {  
  52.   Console.Write(i + " ");  
  53.   if (sleeper.ThreadState == ThreadState.WaitSleepJoin)  
  54.   {  
  55.   Console.WriteLine("Interrupting the sleeping thread.");  
  56.   sleeper.Interrupt();  
  57.   }  
  58.   }  
  59.   }  
  60.   }  
  61.   } 

 

  在上面的例子中,当计数器的值为10, 20 和 30 时第一个线程会睡眠。第二个线程会检查第一个线程是否已经进入睡眠状态。如果是的话,它将中断第一个线程并使它回到调度队列中去。Interrupt()方法是让睡眠线程重新醒来的最好方式,当线程等待的资源可用且你想让线程继续运行时你可以使用这个方法。输出结果与下面显示的类似:

原文链接:http://www.cnblogs.com/danielWise/archive/2012/01/15/2323084.html

【编辑推荐】

  1. .NET 4多核并行中的Task优化线程池
  2. .NET跨线程控件的相关操作
  3. 详解.NET多线程异常的处理方法
  4. 讲述ADO多线程如何更好的进行操作
责任编辑:彭凡 来源: 博客园
相关推荐

2023-10-26 08:25:35

Java线程周期

2010-07-14 10:48:37

Perl线程

2013-08-19 17:03:00

.Net生命周期对象

2009-06-18 13:32:39

Java线程生命周期

2009-06-29 18:03:15

Java多线程线程的生命周期

2009-07-23 10:23:44

2010-07-14 10:59:15

Perl线程

2009-07-23 18:14:17

MVC生命周期

2013-04-07 10:42:56

Asp.Net页面周期

2009-07-20 10:33:02

ASP.NET MVC

2015-07-08 16:28:23

weak生命周期

2009-07-31 17:53:39

ASP.NET线程安全

2022-04-19 07:20:24

软件开发安全生命周期SSDLC应用安全

2009-08-04 17:49:31

Web Page生命周ASP.NET Pos

2009-08-04 16:05:15

ASP.NET页面生命

2009-07-31 10:47:18

ASP.NET页面生命

2009-08-04 16:50:15

ASP.NET页面生命

2009-06-11 11:28:35

JSF生命周期

2009-02-12 13:16:55

请求生命周期MVCASP.NET

2010-01-15 13:52:44

VB.NET对象生命周
点赞
收藏

51CTO技术栈公众号