码迷,mamicode.com
首页 > 编程语言 > 详细

.NET并行计算和并发6-获取线程池的最大可用线程数

时间:2017-08-18 18:28:00      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:next   setevent   ini   mod   auto   item   readline   buffers   his   

  1 using System;
  2 using System.IO;
  3 using System.Security.Permissions;
  4 using System.Threading;
  5 
  6 class Test
  7 {
  8     static void Main()
  9     {
 10         AutoResetEvent mainEvent = new AutoResetEvent(false);
 11         int workerThreads;
 12         int portThreads;
 13 
 14         ThreadPool.GetMaxThreads(out workerThreads, out portThreads);
 15         Console.WriteLine("\nMaximum worker threads: \t{0}" +
 16             "\nMaximum completion port threads: {1}",
 17             workerThreads, portThreads);
 18 
 19         ThreadPool.GetAvailableThreads(out workerThreads,
 20             out portThreads);
 21         Console.WriteLine("\nAvailable worker threads: \t{0}" +
 22             "\nAvailable completion port threads: {1}\n",
 23             workerThreads, portThreads);
 24 
 25         ThreadPool.QueueUserWorkItem(new
 26             WaitCallback(ThreadPoolTest.WorkItemMethod), mainEvent);
 27 
 28         // Since ThreadPool threads are background threads, 
 29         // wait for the work item to signal before ending Main.
 30         mainEvent.WaitOne(5000, false);
 31         Console.ReadLine();
 32     }
 33 }
 34 
 35 class ThreadPoolTest
 36 {
 37     // Maintains state information to be passed to EndWriteCallback.
 38     // This information allows the callback to end the asynchronous
 39     // write operation and signal when it is finished.
 40     class State
 41     {
 42         public FileStream fStream;
 43         public AutoResetEvent autoEvent;
 44 
 45         public State(FileStream fStream, AutoResetEvent autoEvent)
 46         {
 47             this.fStream = fStream;
 48             this.autoEvent = autoEvent;
 49         }
 50     }
 51 
 52     ThreadPoolTest() { }
 53 
 54     public static void WorkItemMethod(object mainEvent)
 55     {
 56         Console.WriteLine("\nStarting WorkItem.\n");
 57         AutoResetEvent autoEvent = new AutoResetEvent(false);
 58 
 59         // Create some data.
 60         const int ArraySize = 10000;
 61         const int BufferSize = 10000;
 62         byte[] byteArray = new Byte[ArraySize];
 63         new Random().NextBytes(byteArray);
 64 
 65         // Create two files and two State objects. 
 66         FileStream fileWriter1 =
 67             new FileStream(@"C:\Test1@##.dat", FileMode.Create,
 68             FileAccess.ReadWrite, FileShare.ReadWrite,
 69             BufferSize, true);
 70         FileStream fileWriter2 =
 71             new FileStream(@"C:\Test2@##.dat", FileMode.Create,
 72             FileAccess.ReadWrite, FileShare.ReadWrite,
 73             BufferSize, true);
 74         State stateInfo1 = new State(fileWriter1, autoEvent);
 75         State stateInfo2 = new State(fileWriter2, autoEvent);
 76 
 77         // Asynchronously write to the files.
 78         fileWriter1.BeginWrite(byteArray, 0, byteArray.Length,
 79             new AsyncCallback(EndWriteCallback), stateInfo1);
 80         fileWriter2.BeginWrite(byteArray, 0, byteArray.Length,
 81             new AsyncCallback(EndWriteCallback), stateInfo2);
 82 
 83         // Wait for the callbacks to signal.
 84         autoEvent.WaitOne();
 85         autoEvent.WaitOne();
 86 
 87         fileWriter1.Close();
 88         fileWriter2.Close();
 89         Console.WriteLine("\nEnding WorkItem.\n");
 90 
 91         // Signal Main that the work item is finished.
 92         ((AutoResetEvent)mainEvent).Set();
 93     }
 94 
 95     static void EndWriteCallback(IAsyncResult asyncResult)
 96     {
 97         Console.WriteLine("Starting EndWriteCallback.");
 98 
 99         State stateInfo = (State)asyncResult.AsyncState;
100         int workerThreads;
101         int portThreads;
102         try
103         {
104             ThreadPool.GetAvailableThreads(out workerThreads,
105                 out portThreads);
106             Console.WriteLine("\nAvailable worker threads: \t{0}" +
107                 "\nAvailable completion port threads: {1}\n",
108                 workerThreads, portThreads);
109 
110             stateInfo.fStream.EndWrite(asyncResult);
111 
112             // Sleep so the other thread has a chance to run
113             // before the current thread ends.
114             //Thread.Sleep(1500);
115         }
116         finally
117         {
118             // Signal that the current thread is finished.
119             stateInfo.autoEvent.Set();
120             Console.WriteLine("Ending EndWriteCallback.");
121         }
122     }
123 }
124 

.NET并行计算和并发6-获取线程池的最大可用线程数

标签:next   setevent   ini   mod   auto   item   readline   buffers   his   

原文地址:http://www.cnblogs.com/daigualu/p/7390810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!