System.Threading.ThreadPool类实现了线程池。ThreadPool类是一个静态类,它提供了管理线程池的一系列方法
ThreadPool中的一些属性方法的意义
ThreadPool.GetMaxThreads(out int ,out int)和ThreadPool.SetMaxThreads(int, int);
---------------可以获取和设置线程池的最大线程数,即线程池中共有多少个线程。线程池中的线程数目仅受可用内存的限制。但是,线程池将对允许在进程中同时处于活动状态的线程数目强制实施限制(这取决于CPU的数目和其他因素)。默认情况下,每个系统处理器最多可以运行25个线程池线程。
ThreadPool.GetMinThreads(out int, out int)和ThreadPool.SetMinThreads(int, int);
----------------可以获取和设置线程池所维持的空闲线程数,意思:当线程池处于空闲状态时,线程池中保留多少个线程,以便在有需求时,队列任务可以立即启动。
ThreadPool.GetAvailableThreads(out int, out int);
---------------通过此方法,可以获取当前时刻下,可以使用的线程数目。
QueueUserWorkItem(WaitCallback, Object) 方法和QueueUserWorkItem(WaitCallback)
----------------这些方法将“工作项”(和Object是要传入WaitCallback中的参数)插入到线程池的队列中,并立即返回。工作项只是一种方法(就是WaitCallback类型的委托),它被调用并传递给单个参数(也可以没有参数)。没有参数的QueueUserWorkItem版本将null传递给回调方法。线程池中的某些线程将调用System.Threading.WaitCallback委托表示的回调方法来处理该工作项。回调方法必须与System.Threading.WaitCallback委托类型相匹配。WaitCallback定义如下:
public delegate void WaitCallback(Object state);
调用QueueUserWorkItem时传入的Object类型参数将传递到任务过程,可以通过这种方式来向任务过程传递参数。如果任务过程需要多个参数,可以定义包含这些数据的类,并将类的实例强制转换为Object数据类型。
下面是我学习线程池的代码,用于简单测试这些方法的意义。
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Demo_ThreadPool
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class Demo_ThreadPool
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static void Main()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int MaxNum,portThreadNum;
int MinNum;
int num = 5;
ManualResetEvent[] doneEvents = new ManualResetEvent[num];
Fibonacci[] fibonaccis = new Fibonacci[num];
Random r = new Random();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ThreadPool中的参数没有设置时的状况#region ThreadPool中的参数没有设置时的状况
ThreadPool.GetMaxThreads(out MaxNum, out portThreadNum);
Console.WriteLine(MaxNum + "-------------------" + portThreadNum);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ThreadPool.GetMinThreads(out MinNum, out portThreadNum);
Console.WriteLine(MinNum + "------------------------" + portThreadNum);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ThreadPool.GetAvailableThreads(out MaxNum, out portThreadNum);
Console.WriteLine(MaxNum + "-------------------" + portThreadNum);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < num; i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
doneEvents[i] = new ManualResetEvent(false);
fibonaccis[i] = new Fibonacci(r.Next(10, 40), doneEvents[i]);
ThreadPool.QueueUserWorkItem(fibonaccis[i].ThreadPoolCallback, i);
ThreadPool.GetAvailableThreads(out MaxNum, out portThreadNum);
Console.WriteLine(MaxNum + "-------------------" + portThreadNum);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
设置ThreadPool中的线程数的状况#region 设置ThreadPool中的线程数的状况
ThreadPool.SetMaxThreads(20, 40);
ThreadPool.SetMinThreads(1, 1);
ThreadPool.GetMaxThreads(out MaxNum, out portThreadNum);
Console.WriteLine(MaxNum + "-------------------" + portThreadNum);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ThreadPool.GetMinThreads(out MinNum, out portThreadNum);
Console.WriteLine(MinNum + "------------------------" + portThreadNum);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ThreadPool.GetAvailableThreads(out MaxNum, out portThreadNum);
Console.WriteLine(MaxNum + "-------------------" + portThreadNum);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < num; i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
doneEvents[i] = new ManualResetEvent(false);
fibonaccis[i] = new Fibonacci(r.Next(10, 40), doneEvents[i]);
ThreadPool.QueueUserWorkItem(fibonaccis[i].ThreadPoolCallback, i);
ThreadPool.GetAvailableThreads(out MaxNum, out portThreadNum);
Console.WriteLine(MaxNum + "-------------------" + portThreadNum);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.Read();
}
static void testReset()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i = 0;
while (true)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("in the front of "+i++);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
}
public class Fibonacci
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Fibonacci(int n, ManualResetEvent doneEvent)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_n = n;
_doneEvent = doneEvent;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Wrapper method for use with thread pool.
public void ThreadPoolCallback(Object threadContext)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int threadIndex = (int)threadContext;
Console.WriteLine("thread {0} started
", threadIndex);
_fibOfN = Calculate(_n);
Console.WriteLine("thread {0} result calculated
", threadIndex);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_doneEvent.Set();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Recursive method that calculates the Nth Fibonacci number.
public int Calculate(int n)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (n <= 1)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return n;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Calculate(n - 1) + Calculate(n - 2);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int N
{ get
{ return _n; } }
private int _n;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int FibOfN
{ get
{ return _fibOfN; } }
private int _fibOfN;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private ManualResetEvent _doneEvent;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}