SmartThreadPool

时间:2022-06-03 20:49:10

https://github.com/amibar/SmartThreadPool

首先是实例化的时候的参数的解释

//Initialize SmartThreadPool & Make logs
//SmartThreadPool m_hThreadPool;
//m_hThreadPool = new SmartThreadPool();//声明一个线程池
STPStartInfo stp = new STPStartInfo();//线程详细配置参数
//m_hThreadPool.STPStartInfo这个属性是只读属性,所以只能在实例化的时候设置
{
stp.AsReadOnly();//返回一个只读类型的STPStartInfo
//一个枚举值,储存工作项执行完成后是否调用回调方法,
//Never不调用,
//WhenWorkItemCanceled只有当工作项目被取消时调用
//WhenWorkItemNotCanceled只有当工作项目不取消调用
//Always调用
stp.CallToPostExecute = CallToPostExecute.Always;//在这里选择总是回调
//当工作项执行完成后,是否释放工作项的参数,如果释放,参数对象必须实现IDisposable接口
stp.DisposeOfStateObjects = true;
//当线程池中没有工作项时,闲置的线程等待时间,超过这个时间后,会释放掉这个闲置的线程,默认为60秒
stp.IdleTimeout = ;//300s
//最大线程数,默认为25,
//注意,由于windows的机制,所以一般最大线程最大设置成25,
//如果设置成0的话,那么线程池将停止运行
stp.MaxWorkerThreads = ;//15 thread
//只在STP执行Action<...>与Func<...>两种任务时有效
//在执行工作项的过程中,是否把参数传递到WorkItem中去,用做IWorkItemResult接口取State时使用,
//如果设置为false那么IWorkItemResult.State是取不到值的
//如果设置为true可以取到传入参数的数组
stp.FillStateWithArgs = true;
//最小线程数,默认为0,当没有工作项时,线程池最多剩余的线程数
stp.MinWorkerThreads = ;//5 thread
//当工作项执行完毕后,默认的回调方法
stp.PostExecuteWorkItemCallback = delegate(IWorkItemResult wir) { MessageBox.Show("ok" + wir.Result); };
//是否需要等待start方法后再执行工作项,?默认为true,当true状态时,STP必须执行Start方法,才会为线程分配工作项
stp.StartSuspended = true;
}
m_hThreadPool = new SmartThreadPool(stp);//带线程初始化的线程池初始化

以下是使用两种方法定义函数运行等待返回结果的演示,一种是等待实例化中的对象全部执行完成,一种是等待其中的某些执行完成。

private void button1_Click(object sender, EventArgs e)
{ //这个例子将演示传入数个参数并且等待运行然后传出的全过程
SmartThreadPool stp = new SmartThreadPool();
IWorkItemResult<string> resultCallback = stp.QueueWorkItem(new Amib.Threading.Func<string, string, string>(GetResultstring), "hello ", "world");
stp.Start();
stp.WaitForIdle();//等待该实例下的所有结果返回
MessageBox.Show(resultCallback.Result);
stp.Shutdown();
} private string GetResultstring(string str, string str2)
{
return str + str2;
}
private void button2_Click(object sender, EventArgs e)
{
//这个例子将演示一批参数的传入一批线程并且等待执行结束返回值
SmartThreadPool stp = new SmartThreadPool();
List<IWorkItemResult> t_lResultItem = new List<IWorkItemResult>();//不对IWorkItemResult定义其类型,其结果需要自己做类型转换
for (int step = ; step != ;step++ )
{
//这里使用另一种方法来做函数
t_lResultItem.Add(stp.QueueWorkItem(new WorkItemCallback(GetObjectString), new string[] { "hello ", step.ToString() }));
}
stp.Start();
//等待所需的结果返回
if (SmartThreadPool.WaitAll(t_lResultItem.ToArray()))
{
foreach (IWorkItemResult t in t_lResultItem)
{
MakeLog(string.Format("{0}{1}", t.State, t.Result));
}
}
} private object GetObjectString(object obj)
{
return string.Format("{0}{1}", (obj as string[])[], (obj as string[])[]);
}

处理线程执行过程中出现的错误

private void button3_Click(object sender, EventArgs es)
{
//处理线程执行过程中出现的错误
SmartThreadPool stp = new SmartThreadPool();//如果需要将线程池设置为调用start的时候才运行,需要设置其StartSuspended参数为true,然后为其调用start方法来启动
IWorkItemResult<double> ret = stp.QueueWorkItem(new Amib.Threading.Func<double, double, double>(Diverse), 10.0, );
//接收错误的句柄
stp.Start();
Exception e = null;
double resule = ret.GetResult(out e);//在取出结果的时候判断是否有错误产生
if (e != null)
{
//在这里进行错误处理,错误在InnerException中
MessageBox.Show(e.InnerException.Message);
}
else
{
MessageBox.Show(resule.ToString());
}
stp.Shutdown();
} private double Diverse(double x, double y)
{
return x/y;
}

使用线程分组

private void button4_Click(object sender, EventArgs e)
{
//这里演示了线程的分组
SmartThreadPool stp = new SmartThreadPool();
//创建一个分组并用这个分组管理
IWorkItemsGroup mainGroup = stp.CreateWorkItemsGroup();//如果需要设置这个分组为调用start的时候才开始运行,需要传入WIGStartInfo参数,将其参数中的StartSuspended设置为true然后调用分组的start方法
//向分组中添加任务->当然可以有返回值
mainGroup.QueueWorkItem(new WorkItemCallback(GetObjectString), );
//分组等待所有任务完成
mainGroup.WaitForIdle();
//关闭
stp.Shutdown();
}

以下是SmartThreadPool的部分解释

SmartThreadPool smartThreadPool = new SmartThreadPool();

//获取当前线程池中的工作线程数,与InUseThreads可能会有差别,因为InUseThreads不包含Idle状态的线程
int threadNum = smartThreadPool.ActiveThreads;
//取消所有工作项,如果工作项在执行,那么等待工作项执行完
smartThreadPool.Cancel();
//如果不想等待工作项执行完,
smartThreadPool.Cancel(true);
//线程池的最大并发数,即MaxWorkerThreads,
//如果修改后的Concurrency小于MinWorkerThreads,那么MinWorkerThreads也会随之改变
smartThreadPool.Concurrency = ;
//创建一个工作组,最大并发为3,工作组在后面会详细说明,
smartThreadPool.CreateWorkItemsGroup();
//卸载线程池
smartThreadPool.Dispose();
//反回所有未执行的工作项的参数对象
smartThreadPool.GetStates();
//获取线程池中正在工作的线程数,与ActiveThreads会有差别,因为ActiveThreads可能包含Idle状态的线程
int useThreadNum = smartThreadPool.InUseThreads;
//当线程池用没有工作项时,反回true,否则,反回false
bool IsIdle = smartThreadPool.IsIdle;
//同时并行执行多个方法,并且阻塞到所有工作项都执行完,这里会有多少个工作项就会创造多少个线程,
smartThreadPool.Join(new Action[] { new Action(Test) });
//获取或设置最大线程数,即MaxWorkerThreads,
smartThreadPool.MaxThreads = ;
//最小线程数,当没有工作项时,线程池最多剩余的线程数
smartThreadPool.MinThreads = ;
//线程池的名称,没什么特殊的用处,
smartThreadPool.Name = "StartThreadPool";
//当线程池中没有工作项(即闲置)时触发的事件
smartThreadPool.OnIdle += new WorkItemsGroupIdleHandler(smartThreadPool_OnIdle);
//当线程池启动一个线程时,触发的事件
smartThreadPool.OnThreadInitialization += new ThreadInitializationHandler(smartThreadPool_OnThreadInitialization);
//当线程池释放一个线程时,所触发的事件
smartThreadPool.OnThreadTermination += new ThreadTerminationHandler(smartThreadPool_OnThreadTermination);
//与Join方法类似,并行执行多个带参数的方法,这里会有多少个工作项就会创造多少个线程
smartThreadPool.Pipe<object>(new object(), new Action<object>[] { new Action<object>(Test) });
//卸载线程池
smartThreadPool.Shutdown();
//启动线程池
smartThreadPool.Start();
//STPStartInfo对象的只读实例
STPStartInfo stpStartInfo = smartThreadPool.STPStartInfo;
//等待所有的工作项执行完成(即IsIdle为true)
smartThreadPool.WaitForIdle();
//获取还未执行的工作项数量
int wiNum = smartThreadPool.WaitingCallbacks;
//WorkItemGroup的启动信息的只读实力
WIGStartInfo wigStartInfo = smartThreadPool.WIGStartInfo;

****************

1、为什么需要使用线程池(Thread Pool)

  • 减少线程间上下文切换。线程执行一定的时间片后,系统会自动把cpu切换给另一个线程使用,这时还需要保存当 前的线程上下文状态,并加载新线程的上下文状态。当程序中有大量的线程时,每个线程分得的时间片会越来越少,可能会出现线程未处理多少操作,就需要切换到 另一线程,这样频繁的线程间上下文切换会花费大量的cpu时间。
  • 减少内存占用。系统每创建一条物理线程,需要大概花费1MB的内存空间,许多程序喜欢先创建多条物理线程,并 周期轮询来处理各自的任务,这样既消耗了线程上下文切换的时间,还浪费了内存。这些任务可能只需要一条线程就能满足要求。假如某一任务需要执行较长的周 期,线程池还可以自动增加线程,并在空闲时,销毁线程,释放占用的内存。

2、为什么不使用.Net默认的线程池

  • .Net默认的线程池(ThreadPool)是一个静态类,所以是没办法自己创建一个新的程序池的。默认的线程池与应用程序域 (AppDomain)挂钩,一个AppDomain只有一个线程池。假如在线程池中执行了一个周期较长的任务,一直占用着其中一个线程,可能就会影响到 应用程序域中的其他程序的性能。例如,假如在Asp.Net的线程池中执行一个周期较长的任务,就会影响请求的并发处理能力(线程池默认有个最大线程 数)。 3、SmartThreadPool特性和优点

SmartThreadPool特性如下:

  • 池中的线程数量会根据负载自动增减
  • 任务异步执行后可以返回值
  • 处于任务队列中未执行的任务可以取消
  • 回调函数可以等待多个任务都执行完成后再触发
  • 任务可以有优先级(priority)
  • 任务可以分组
  • 支持泛型Action<T> 和 Func<T>
  • 有性能监测机制

4、使用示例 最简单的使用方法:

1
2
3
4
5
6
7
8
// 创建一个线程池
SmartThreadPool smartThreadPool = new SmartThreadPool();
 
 // 执行任务
smartThreadPool.QueueWorkItem(() =>
{
     Console.WriteLine("Hello World!");
});

带返回值的任务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 创建一个线程池
SmartThreadPool smartThreadPool = new SmartThreadPool();
 
// 执行任务
var result = smartThreadPool.QueueWorkItem(() =>
{
    var sum = 0;
    for (var i = 0; i < 10; i++)
        sum += i;
 
    return sum;
});
 
// 输出计算结果
Console.WriteLine(result.Result);

等待多个任务执行完成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 创建一个线程池
SmartThreadPool smartThreadPool = new SmartThreadPool();
 
// 执行任务
var result1 = smartThreadPool.QueueWorkItem(() =>
{
    //模拟计算较长时间
    Thread.Sleep(5000);
 
    return 3;
});
 
var result2 = smartThreadPool.QueueWorkItem(() =>
{
    //模拟计算较长时间
    Thread.Sleep(3000);
 
    return 5;
});
 
bool success = SmartThreadPool.WaitAll(
    new IWorkItemResult[] { result1, result2 });
 
if (success)
{
    // 输出结果
    Console.WriteLine(result1.Result);
    Console.WriteLine(result2.Result);
}

5、结论 使用SmartThreadPool可以简单就实现支持多线程的程序,由线程池来管理线程,可以减少死锁的出现。SmartThreadPool还支持简单的生产者-消费者模式,当不需要对任务进行持久化时,还是很好用的。

6、扩展阅读 http://www.codeproject.com/KB/threads/smartthreadpool.aspx

http://smartthreadpool.codeplex.com/

http://www.albahari.com/threading/

相关文章