聊聊Thread类线程常用操作

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

本文转载自微信公众号「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 

 

责任编辑:武晓燕 来源: UP技术控
相关推荐

2021-06-29 07:04:16

Sed常用操作

2009-06-29 17:54:10

Java多线程Thread类创建线程

2020-12-28 08:03:26

多线程进程浏览器

2020-09-08 15:14:51

线程 APIs周期

2021-11-26 00:02:00

模式正则修饰符

2023-04-02 17:53:10

多线程编程自测

2009-08-04 17:08:12

C# Thread类

2009-01-04 11:55:09

Java数组Java常用工具Java类

2021-05-10 16:27:01

μCOSFreeRTOS

2023-08-30 07:45:28

Python管理安全性

2020-02-26 15:12:43

线程池增长回收

2023-09-01 08:59:57

2022-02-07 11:55:00

linux进程线程

2009-08-18 15:31:07

C# 操作Excel

2024-04-17 09:52:00

操作系统多线程内存

2024-03-12 13:11:20

powerjob单机线程

2010-03-05 13:53:38

Python Thre

2021-08-16 06:56:21

Slice数组类型内存

2009-08-28 15:37:22

C#线程类的定义

2022-08-29 10:52:37

线程函数操作系统
点赞
收藏

51CTO技术栈公众号