偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

聊聊Thread類線程常用操作

開發(fā) 前端
線程是通過擴展 Thread 類創(chuàng)建的。擴展的 Thread 類調(diào)用 Start() 方法來開始子線程的執(zhí)行。

本文轉(zhuǎn)載自微信公眾號「UP技術(shù)控」,作者conan5566。轉(zhuǎn)載本文請聯(lián)系UP技術(shù)控公眾號。   

創(chuàng)建線程

線程是通過擴展 Thread 類創(chuàng)建的。擴展的 Thread 類調(diào)用 Start() 方法來開始子線程的執(zhí)行。

下面的程序演示了這個概念:

  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.     } 

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

  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.     } 

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

  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.                 // 計數(shù)到 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.             // 現(xiàn)在中止子線程 
  38.             Console.WriteLine("In Main: Aborting the Child thread"); 
  39.             childThread.Abort(); 
  40.             Console.ReadKey(); 
  41.         } 
  42.     } 

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

  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 

 

責(zé)任編輯:武曉燕 來源: UP技術(shù)控
相關(guān)推薦

2021-06-29 07:04:16

Sed常用操作

2024-05-16 12:51:15

WinForms線程UI

2009-06-29 17:54:10

Java多線程Thread類創(chuàng)建線程

2020-12-28 08:03:26

多線程進(jìn)程瀏覽器

2020-09-08 15:14:51

線程 APIs周期

2021-11-26 00:02:00

模式正則修飾符

2023-04-02 17:53:10

多線程編程自測

2023-09-01 08:59:57

2024-05-27 00:27:59

WinForm線程應(yīng)用程序

2023-08-30 07:45:28

Python管理安全性

2021-05-10 16:27:01

μCOSFreeRTOS

2009-01-04 11:55:09

Java數(shù)組Java常用工具Java類

2022-02-07 11:55:00

linux進(jìn)程線程

2009-08-04 17:08:12

C# Thread類

2020-02-26 15:12:43

線程池增長回收

2009-08-18 15:31:07

C# 操作Excel

2021-08-16 06:56:21

Slice數(shù)組類型內(nèi)存

2024-04-17 09:52:00

操作系統(tǒng)多線程內(nèi)存

2024-03-12 13:11:20

powerjob單機線程

2021-03-24 09:37:41

數(shù)據(jù)類型數(shù)據(jù)分析數(shù)據(jù)的分類
點贊
收藏

51CTO技術(shù)棧公眾號