聊聊Thread类线程常用操作

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

伊吾网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。成都创新互联公司自2013年起到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司

创建线程

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

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

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

本文名称:聊聊Thread类线程常用操作
网页URL:http://www.shufengxianlan.com/qtweb/news45/258395.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联