您的位置:首页 > 其它

线程池控制并发数量

2016-11-10 14:21 204 查看
直接限定线程池的最大最小线程数也可以, 但会影响程序其它地方用到线程池的地方,因为线程池的设置是全局的。

这篇文章非常不错, 推荐大家看看:点击打开链接

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication15
{

public class Program
{
static void Main(string[] args)
{
List<Single> list = new List<Single>();
list.Add(new Single(1, 1));
list.Add(new Single(2, 2));
list.Add(new Single(3, 1));
list.Add(new Single(4, 2));
list.Add(new Single(5, 1));
list.Add(new Single(6, 2));
list.Add(new Single(7, 2));
list.Add(new Single(8, 1));
list.Add(new Single(9, 2));
list.Add(new Single(10, 1));
list.ForEach(p => p.ItemList.AddRange(list));

// 新建 ManualResetEvent 对象并且初始化为无信号状态
ManualResetEvent eventX = new ManualResetEvent(false);

//初始仅加入2个任务
int maxThreadsCount = 2;
for (int i = 0; i < maxThreadsCount; i++)
{
Single item = list[i];
item.EventX = eventX;
ThreadPool.QueueUserWorkItem(new WaitCallback(item.Exec), null);
}

//等待事件的完成,即线程调用 ManualResetEvent.Set() 方法
eventX.WaitOne(Timeout.Infinite, true);

for (int i = 0; i < maxThreadsCount; i++)
{
Single item = list[i];
item.PrintFlag();
Console.WriteLine();
}
Console.ReadKey();
}
}//end of class Program

public class Single
{
#region [属性]
public int Flag { get; set; }
public int NextFlag { get; set; }
public DateTime BeginTime { get; set; }
public DateTime EndTime { get; set; }
public int WaitMs { get; set; }
public bool Pending { get; set; }
public bool Finished { get; set; }
public List<Single> ItemList { get; set; }
public ManualResetEvent EventX { get; set; }

public double ElaspedSeconds
{
get
{
return this.EndTime.Subtract(this.BeginTime).TotalSeconds;
}
}
#endregion

public Single(int _flag, int _waitSeconds)
{
this.Flag = _flag;
this.WaitMs = _waitSeconds * 1000;
this.Pending = false;
this.Finished = false;
this.ItemList = new List<Single>();
}

public void Exec(object obj)
{
this.Pending = true;
this.BeginTime = DateTime.Now;
Thread.Sleep(this.WaitMs);
this.EndTime = DateTime.Now;
this.Finished = true;

lock (this.ItemList)
{
Console.WriteLine("Flag:{0}, BeginTime:{1:HH:mm:ss,ms}, EndTime: {2:HH:mm:ss,ms}, ElapsedSeconds:{3}"
, this.Flag < 10 ? " " + this.Flag.ToString() : this.Flag.ToString()
, this.BeginTime
, this.EndTime
, this.ElaspedSeconds
);
Single item = ItemList.Find(p => !p.Pending);
//如果全部都运行过了
if (item == null)
{
//如果全部都运行完了
if (ItemList.Find(p => !p.Finished) == null)
{
//注:此处设置状态为终止
this.EventX.Set();
}
return;
}

this.NextFlag = item.Flag;
//注意此处要赋值,否则会null异常
item.EventX = this.EventX;
ThreadPool.QueueUserWorkItem(new WaitCallback(item.Exec), null);
}
}

public void PrintFlag()
{
if (this.NextFlag == 0)
{
Console.WriteLine("{0}, WaitSeconds:{2}", this.Flag, this.NextFlag, this.WaitMs / 1000);
return;
}

Console.WriteLine("{0}->{1}, WaitSeconds:{2}", this.Flag, this.NextFlag, this.WaitMs/1000);
Single next = this.ItemList.Find(p => p.Flag == this.NextFlag);
if (next != null)
{
next.PrintFlag();
}
}
}//end of class Single
}//end of namespace



分配得很好,赞!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: