何謂 C# 執行緒集區 Thread Pool,以及其運作方式探討
執行緒集區的相關設定資訊
邏輯處理器數目 : 8
WorkItem Thread : (Busy:0, Free:32767, Min:8, Max:32767)
IOPC Thread : (Busy:0, Free:1000, Min:8, Max:1000)
int workerThreads;
int completionPortThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
threadPoolInformation.AvailableWorkerThreads = workerThreads;
threadPoolInformation.AvailableCompletionPortThreads = completionPortThreads;
ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
threadPoolInformation.MaxWorkerThreads = workerThreads;
threadPoolInformation.MaxCompletionPortThreads = completionPortThreads;
ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
threadPoolInformation.MinWorkerThreads = workerThreads;
threadPoolInformation.MinCompletionPortThreads = completionPortThreads;
threadPoolInformation.ProcessorCount= System.Environment.ProcessorCount;
執行緒集區運作方式的測試方法說明
當要產生出低於執行緒集區預設建立的執行緒數量
執行緒集區的相關設定資訊
邏輯處理器數目 : 8
WorkItem Thread : (Busy:0, Free:32767, Min:8, Max:32767)
IOPC Thread : (Busy:0, Free:1000, Min:8, Max:1000)
準備產生出 8 個執行緒
請按下任一按鍵,進行執行緒集區的使用模擬
執行緒[1]開始: ID=4, time=09:34:49.0166567
WorkItem Thread : (Busy:1, Free:32766, Min:8, Max:32767)
執行緒[2]開始: ID=5, time=09:34:49.0269256
WorkItem Thread : (Busy:3, Free:32764, Min:8, Max:32767)
執行緒[3]開始: ID=6, time=09:34:49.0379935
WorkItem Thread : (Busy:4, Free:32763, Min:8, Max:32767)
執行緒[4]開始: ID=7, time=09:34:49.0489234
WorkItem Thread : (Busy:5, Free:32762, Min:8, Max:32767)
執行緒[5]開始: ID=8, time=09:34:49.0599068
WorkItem Thread : (Busy:6, Free:32761, Min:8, Max:32767)
執行緒[6]開始: ID=9, time=09:34:49.0708600
WorkItem Thread : (Busy:7, Free:32760, Min:8, Max:32767)
執行緒[7]開始: ID=10, time=09:34:49.0819126
WorkItem Thread : (Busy:8, Free:32759, Min:8, Max:32767)
執行緒[8]開始: ID=11, time=09:34:49.0928839
WorkItem Thread : (Busy:8, Free:32759, Min:8, Max:32767)
執行緒[1]結束: ID=4, time=09:34:57.0179431
WorkItem Thread : (Busy:8, Free:32759, Min:8, Max:32767)
執行緒[2]結束: ID=5, time=09:34:57.0279241
WorkItem Thread : (Busy:7, Free:32760, Min:8, Max:32767)
執行緒[3]結束: ID=6, time=09:34:57.0389303
WorkItem Thread : (Busy:6, Free:32761, Min:8, Max:32767)
執行緒[4]結束: ID=7, time=09:34:57.0499451
WorkItem Thread : (Busy:5, Free:32762, Min:8, Max:32767)
執行緒[5]結束: ID=8, time=09:34:57.0609511
WorkItem Thread : (Busy:4, Free:32763, Min:8, Max:32767)
執行緒[6]結束: ID=9, time=09:34:57.0719209
WorkItem Thread : (Busy:3, Free:32764, Min:8, Max:32767)
執行緒[7]結束: ID=10, time=09:34:57.0829550
WorkItem Thread : (Busy:2, Free:32765, Min:8, Max:32767)
執行緒[8]結束: ID=11, time=09:34:57.0939583
WorkItem Thread : (Busy:1, Free:32766, Min:8, Max:32767)
當要產生出高於執行緒集區預設建立的執行緒數量
執行緒集區的相關設定資訊
邏輯處理器數目 : 8
WorkItem Thread : (Busy:0, Free:32767, Min:8, Max:32767)
IOPC Thread : (Busy:0, Free:1000, Min:8, Max:1000)
準備產生出 12 個執行緒
請按下任一按鍵,進行執行緒集區的使用模擬
執行緒[1]開始: ID=4, time=09:45:22.7045868
WorkItem Thread : (Busy:1, Free:32766, Min:8, Max:32767)
執行緒[2]開始: ID=5, time=09:45:22.7139011
WorkItem Thread : (Busy:3, Free:32764, Min:8, Max:32767)
執行緒[3]開始: ID=6, time=09:45:22.7248548
WorkItem Thread : (Busy:4, Free:32763, Min:8, Max:32767)
執行緒[4]開始: ID=7, time=09:45:22.7358487
WorkItem Thread : (Busy:5, Free:32762, Min:8, Max:32767)
執行緒[5]開始: ID=8, time=09:45:22.7468800
WorkItem Thread : (Busy:6, Free:32761, Min:8, Max:32767)
執行緒[6]開始: ID=9, time=09:45:22.7578943
WorkItem Thread : (Busy:7, Free:32760, Min:8, Max:32767)
執行緒[7]開始: ID=10, time=09:45:22.7689058
WorkItem Thread : (Busy:7, Free:32760, Min:8, Max:32767)
執行緒[8]開始: ID=11, time=09:45:22.7798759
WorkItem Thread : (Busy:8, Free:32759, Min:8, Max:32767)
執行緒[9]開始: ID=12, time=09:45:23.7050644
WorkItem Thread : (Busy:9, Free:32758, Min:8, Max:32767)
執行緒[10]開始: ID=13, time=09:45:24.7040167
WorkItem Thread : (Busy:10, Free:32757, Min:8, Max:32767)
執行緒[11]開始: ID=14, time=09:45:25.7051190
WorkItem Thread : (Busy:11, Free:32756, Min:8, Max:32767)
執行緒[12]開始: ID=15, time=09:45:26.7052975
WorkItem Thread : (Busy:12, Free:32755, Min:8, Max:32767)
執行緒[1]結束: ID=4, time=09:45:30.7067440
WorkItem Thread : (Busy:12, Free:32755, Min:8, Max:32767)
執行緒[2]結束: ID=5, time=09:45:30.7147327
WorkItem Thread : (Busy:11, Free:32756, Min:8, Max:32767)
執行緒[3]結束: ID=6, time=09:45:30.7257740
WorkItem Thread : (Busy:10, Free:32757, Min:8, Max:32767)
執行緒[4]結束: ID=7, time=09:45:30.7367508
WorkItem Thread : (Busy:9, Free:32758, Min:8, Max:32767)
執行緒[5]結束: ID=8, time=09:45:30.7477686
WorkItem Thread : (Busy:8, Free:32759, Min:8, Max:32767)
執行緒[6]結束: ID=9, time=09:45:30.7587406
WorkItem Thread : (Busy:7, Free:32760, Min:8, Max:32767)
執行緒[7]結束: ID=10, time=09:45:30.7697294
WorkItem Thread : (Busy:6, Free:32761, Min:8, Max:32767)
執行緒[8]結束: ID=11, time=09:45:30.7807757
WorkItem Thread : (Busy:5, Free:32762, Min:8, Max:32767)
執行緒[9]結束: ID=12, time=09:45:31.7057277
WorkItem Thread : (Busy:4, Free:32763, Min:8, Max:32767)
執行緒[10]結束: ID=13, time=09:45:32.7048023
WorkItem Thread : (Busy:3, Free:32764, Min:8, Max:32767)
執行緒[11]結束: ID=14, time=09:45:33.7057667
WorkItem Thread : (Busy:2, Free:32765, Min:8, Max:32767)
執行緒[12]結束: ID=15, time=09:45:34.7077598
WorkItem Thread : (Busy:1, Free:32766, Min:8, Max:32767)
當要產生出高於執行緒集區最大允許的執行緒數量
執行緒集區的相關設定資訊
邏輯處理器數目 : 8
WorkItem Thread : (Busy:0, Free:10, Min:8, Max:10)
IOPC Thread : (Busy:0, Free:10, Min:8, Max:10)
準備產生出 12 個執行緒
請按下任一按鍵,進行執行緒集區的使用模擬
執行緒[1]開始: ID=4, time=10:12:01.9361442
WorkItem Thread : (Busy:1, Free:9, Min:8, Max:10)
執行緒[2]開始: ID=5, time=10:12:01.9461668
WorkItem Thread : (Busy:3, Free:7, Min:8, Max:10)
執行緒[3]開始: ID=6, time=10:12:01.9571457
WorkItem Thread : (Busy:4, Free:6, Min:8, Max:10)
執行緒[4]開始: ID=7, time=10:12:01.9681630
WorkItem Thread : (Busy:5, Free:5, Min:8, Max:10)
執行緒[5]開始: ID=8, time=10:12:01.9792659
WorkItem Thread : (Busy:6, Free:4, Min:8, Max:10)
執行緒[6]開始: ID=9, time=10:12:01.9902236
WorkItem Thread : (Busy:7, Free:3, Min:8, Max:10)
執行緒[7]開始: ID=10, time=10:12:02.0011936
WorkItem Thread : (Busy:8, Free:2, Min:8, Max:10)
執行緒[8]開始: ID=11, time=10:12:02.0111697
WorkItem Thread : (Busy:8, Free:2, Min:8, Max:10)
執行緒[9]開始: ID=12, time=10:12:02.9363821
WorkItem Thread : (Busy:9, Free:1, Min:8, Max:10)
執行緒[10]開始: ID=13, time=10:12:03.9363546
WorkItem Thread : (Busy:10, Free:0, Min:8, Max:10)
執行緒[1]結束: ID=4, time=10:12:09.9380808
WorkItem Thread : (Busy:10, Free:0, Min:8, Max:10)
執行緒[2]結束: ID=5, time=10:12:09.9470285
WorkItem Thread : (Busy:9, Free:1, Min:8, Max:10)
執行緒[11]開始: ID=5, time=10:12:09.9479320
WorkItem Thread : (Busy:9, Free:1, Min:8, Max:10)
執行緒[3]結束: ID=6, time=10:12:09.9580669
WorkItem Thread : (Busy:9, Free:1, Min:8, Max:10)
執行緒[12]開始: ID=6, time=10:12:09.9587256
WorkItem Thread : (Busy:9, Free:1, Min:8, Max:10)
執行緒[4]結束: ID=7, time=10:12:09.9690581
WorkItem Thread : (Busy:9, Free:1, Min:8, Max:10)
執行緒[5]結束: ID=8, time=10:12:09.9800361
WorkItem Thread : (Busy:8, Free:2, Min:8, Max:10)
執行緒[6]結束: ID=9, time=10:12:09.9910460
WorkItem Thread : (Busy:7, Free:3, Min:8, Max:10)
執行緒[7]結束: ID=10, time=10:12:10.0020466
WorkItem Thread : (Busy:6, Free:4, Min:8, Max:10)
執行緒[8]結束: ID=11, time=10:12:10.0120523
WorkItem Thread : (Busy:5, Free:5, Min:8, Max:10)
執行緒[9]結束: ID=12, time=10:12:10.9370610
WorkItem Thread : (Busy:4, Free:6, Min:8, Max:10)
執行緒[10]結束: ID=13, time=10:12:11.9370721
WorkItem Thread : (Busy:3, Free:7, Min:8, Max:10)
執行緒[11]結束: ID=5, time=10:12:17.9500835
WorkItem Thread : (Busy:2, Free:8, Min:8, Max:10)
執行緒[12]結束: ID=6, time=10:12:17.9600530
WorkItem Thread : (Busy:1, Free:9, Min:8, Max:10)
提升執行緒集區預設產生執行緒緒數量
執行緒集區的相關設定資訊
邏輯處理器數目 : 8
WorkItem Thread : (Busy:0, Free:32767, Min:12, Max:32767)
IOPC Thread : (Busy:0, Free:1000, Min:12, Max:1000)
準備產生出 12 個執行緒
請按下任一按鍵,進行執行緒集區的使用模擬
執行緒[1]開始: ID=4, time=10:29:55.1252398
WorkItem Thread : (Busy:1, Free:32766, Min:12, Max:32767)
執行緒[2]開始: ID=5, time=10:29:55.1354606
WorkItem Thread : (Busy:3, Free:32764, Min:12, Max:32767)
執行緒[3]開始: ID=6, time=10:29:55.1464298
WorkItem Thread : (Busy:4, Free:32763, Min:12, Max:32767)
執行緒[4]開始: ID=7, time=10:29:55.1574602
WorkItem Thread : (Busy:5, Free:32762, Min:12, Max:32767)
執行緒[5]開始: ID=8, time=10:29:55.1684434
WorkItem Thread : (Busy:6, Free:32761, Min:12, Max:32767)
執行緒[6]開始: ID=9, time=10:29:55.1793864
WorkItem Thread : (Busy:7, Free:32760, Min:12, Max:32767)
執行緒[7]開始: ID=10, time=10:29:55.1904425
WorkItem Thread : (Busy:8, Free:32759, Min:12, Max:32767)
執行緒[8]開始: ID=11, time=10:29:55.2014103
WorkItem Thread : (Busy:9, Free:32758, Min:12, Max:32767)
執行緒[9]開始: ID=12, time=10:29:55.2125127
WorkItem Thread : (Busy:10, Free:32757, Min:12, Max:32767)
執行緒[10]開始: ID=13, time=10:29:55.2234850
WorkItem Thread : (Busy:10, Free:32757, Min:12, Max:32767)
執行緒[11]開始: ID=14, time=10:29:55.2344311
WorkItem Thread : (Busy:12, Free:32755, Min:12, Max:32767)
執行緒[12]開始: ID=15, time=10:29:55.2453406
WorkItem Thread : (Busy:12, Free:32755, Min:12, Max:32767)
執行緒[1]結束: ID=4, time=10:30:03.1263397
WorkItem Thread : (Busy:12, Free:32755, Min:12, Max:32767)
執行緒[2]結束: ID=5, time=10:30:03.1363742
WorkItem Thread : (Busy:11, Free:32756, Min:12, Max:32767)
執行緒[3]結束: ID=6, time=10:30:03.1473493
WorkItem Thread : (Busy:10, Free:32757, Min:12, Max:32767)
執行緒[4]結束: ID=7, time=10:30:03.1583797
WorkItem Thread : (Busy:9, Free:32758, Min:12, Max:32767)
執行緒[5]結束: ID=8, time=10:30:03.1693653
WorkItem Thread : (Busy:8, Free:32759, Min:12, Max:32767)
執行緒[6]結束: ID=9, time=10:30:03.1803739
WorkItem Thread : (Busy:7, Free:32760, Min:12, Max:32767)
執行緒[7]結束: ID=10, time=10:30:03.1913785
WorkItem Thread : (Busy:6, Free:32761, Min:12, Max:32767)
執行緒[8]結束: ID=11, time=10:30:03.2023760
WorkItem Thread : (Busy:5, Free:32762, Min:12, Max:32767)
執行緒[9]結束: ID=12, time=10:30:03.2133287
WorkItem Thread : (Busy:4, Free:32763, Min:12, Max:32767)
執行緒[10]結束: ID=13, time=10:30:03.2244469
WorkItem Thread : (Busy:3, Free:32764, Min:12, Max:32767)
執行緒[11]結束: ID=14, time=10:30:03.2353793
WorkItem Thread : (Busy:2, Free:32765, Min:12, Max:32767)
執行緒[12]結束: ID=15, time=10:30:03.2473591
WorkItem Thread : (Busy:1, Free:32766, Min:12, Max:32767)