聊聊Thread类线程常用操作

本文转载自微信公众号「UP技术控」,作者conan5566。转载本文请联系UP技术控公众号。   

创建线程

线程是通过扩展 Thread 类创建的。扩展的 Thread 类调用 Start() 方法来开始子线程的执行。

下面的程序演示了这个概念:

 
 
 
 
  1. class ThreadCreationProgram 
  2.     { 
  3.         public static void CallToChildThread() 
  4.         { 
  5.             Console.WriteLine("Child thread starts"); 
  6.         } 
  7.         
  8.         static void Main(string[] args) 
  9.         { 
  10.             ThreadStart childref = new ThreadStart(CallToChildThread); 
  11.             Console.WriteLine("In Main: Creating the Child thread"); 
  12.             Thread childThread = new Thread(childref); 
  13.             childThread.Start(); 
  14.             Console.ReadKey(); 
  15.         } 
  16.     } 

当上面的代码被编译和执行时,它会产生下列结果:

 
 
 
 
  1. In Main: Creating the Child thread 
  2. Child thread starts 

管理线程

Thread 类提供了各种管理线程的方法。

下面的实例演示了 sleep() 方法的使用,用于在一个特定的时间暂停线程。

 
 
 
 
  1. class ThreadCreationProgram 
  2.     { 
  3.         public static void CallToChildThread() 
  4.         { 
  5.             Console.WriteLine("Child thread starts"); 
  6.             // 线程暂停 5000 毫秒 
  7.             int sleepfor = 5000; 
  8.             Console.WriteLine("Child Thread Paused for {0} seconds"
  9.                               sleepfor / 1000); 
  10.             Thread.Sleep(sleepfor); 
  11.             Console.WriteLine("Child thread resumes"); 
  12.         } 
  13.         
  14.         static void Main(string[] args) 
  15.         { 
  16.             ThreadStart childref = new ThreadStart(CallToChildThread); 
  17.             Console.WriteLine("In Main: Creating the Child thread"); 
  18.             Thread childThread = new Thread(childref); 
  19.             childThread.Start(); 
  20.             Console.ReadKey(); 
  21.         } 
  22.     } 

当上面的代码被编译和执行时,它会产生下列结果:

 
 
 
 
  1. In Main: Creating the Child thread 
  2. Child thread starts 
  3. Child Thread Paused for 5 seconds 
  4. Child thread resumes 

销毁线程

Abort() 方法用于销毁线程。

通过抛出 threadabortexception 在运行时中止线程。这个异常不能被捕获,如果有 finally 块,控制会被送至 finally 块。

下面的程序说明了这点:

 
 
 
 
  1. class ThreadCreationProgram 
  2.     { 
  3.         public static void CallToChildThread() 
  4.         { 
  5.             try 
  6.             { 
  7.  
  8.                 Console.WriteLine("Child thread starts"); 
  9.                 // 计数到 10 
  10.                 for (int counter = 0; counter <= 10; counter++) 
  11.                 { 
  12.                     Thread.Sleep(500); 
  13.                     Console.WriteLine(counter); 
  14.                 } 
  15.                 Console.WriteLine("Child Thread Completed"); 
  16.  
  17.             } 
  18.             catch (ThreadAbortException e) 
  19.             { 
  20.                 Console.WriteLine("Thread Abort Exception"); 
  21.             } 
  22.             finally 
  23.             { 
  24.                 Console.WriteLine("Couldn't catch the Thread Exception"); 
  25.             } 
  26.  
  27.         } 
  28.         
  29.         static void Main(string[] args) 
  30.         { 
  31.             ThreadStart childref = new ThreadStart(CallToChildThread); 
  32.             Console.WriteLine("In Main: Creating the Child thread"); 
  33.             Thread childThread = new Thread(childref); 
  34.             childThread.Start(); 
  35.             // 停止主线程一段时间 
  36.             Thread.Sleep(2000); 
  37.             // 现在中止子线程 
  38.             Console.WriteLine("In Main: Aborting the Child thread"); 
  39.             childThread.Abort(); 
  40.             Console.ReadKey(); 
  41.         } 
  42.     } 

当上面的代码被编译和执行时,它会产生下列结果:

 
 
 
 
  1. In Main: Creating the Child thread 
  2. Child thread starts 
  3. In Main: Aborting the Child thread 
  4. Thread Abort Exception 
  5. Couldn't catch the Thread Exception 

 

THE END