如何实现线程队列 test(int i);执行时间不定1~100分钟。开启三个线程。只同时运行三个函数,其余线程等待伪代码for(int i=0;i<99999;i++){test(i);}注意哦,你如果一下子开启99999线程(然后让其等待),会内存溢出。如果你开启的线程太慢可能函数test已经执行完毕我想要的结果是保证三个线程同时运行,使CPU的利用率达到最大值。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Semaphore可以方便实现线程的运行数量,但是怎么控制线程开启速度呢?关键是线程执行时间不确定,使用for循环开启很容易内存溢出 比如,以下代码在amountThread = 11;//线程总量 为娇小值时没问题,大了就会报错。。 private static int poolFlag = 0;//标记 private const int amountThread = 999999;//线程总量 private const int maxThread = 3;//可执行线程最大数量 private static Mutex muxConsole = new Mutex(); public static void Mains() { for (int i = 0; i < amountThread; i++) { // 创建指定数量的线程 // 是线程调用Run方法 // 启动线程 Thread trd = new Thread(new ThreadStart(Run)); trd.Name = "线程" + i; trd.Start(); } } public static void Run() { muxConsole.WaitOne(); //阻塞队列 Interlocked.Increment(ref poolFlag); //标记+1 if (poolFlag != maxThread) //判断是否等于上限 muxConsole.ReleaseMutex(); //如果此线程达不到可执行线程上限,则继续开通,让后面的线程进来 Console.WriteLine("{0} 正在运行....../n", Thread.CurrentThread.Name); Thread.Sleep(5000); //模拟执行 Console.WriteLine("{0} 已经中止....../n", Thread.CurrentThread.Name); //标记-1 Interlocked.Decrement(ref poolFlag); } 有一个标准的模式,把任务放在一个队列中,一般是LinkedList,然后创建三个线程,不断从队列中取出任务执行,直到队列空为止,这个模式有点复杂,但适应能力很强,LinkedList<int> lstTask=new LinkedList<int>;for(int i=0;i<99999;i++){ lstTask.AddLast(i);}...//创建3个线程for(i=0;i<3;i++){ CreateThread();}....public void CreateThread(){ Thread h=new Thread(test);}public void test(){ int nData; lock(lstTask) { if (lstTask.Count==0) return; nData=lstTask.First.Value; lstTask.RemoveFirst(); } ...//对nData处理} 如果是4.0的,可以用Task及其ContinueWith http://blog.csdn.net/sq_zhuyi/article/details/6869661 image转string 求帮忙 毕业设计 进销存管理系统 用C#还是VC? 一个错误,求解答 将Combox显示的值设置到文本框上 如何做 我写的不行 高手指点 谢谢 怎样实现响应键盘回车? 删除行的问题,不能删除,烦。。。。 主从表问题(WINFORM) 如何向已经有数据的XML文件中追加数据? java调用.net写的com组件,在服务器端tomcat发布后,客户端调用失败 HRESULT: 0x8004503A 一些固定的参数信息是存入ini还是注册表里合适 propertyGrid如何设定属性组(category)的排列顺序和组内的排序 如何实现 字符拼接是,占有固定的宽度呢?
关键是线程执行时间不确定,使用for循环开启很容易内存溢出
private static int poolFlag = 0;//标记
private const int amountThread = 999999;//线程总量
private const int maxThread = 3;//可执行线程最大数量
private static Mutex muxConsole = new Mutex(); public static void Mains()
{
for (int i = 0; i < amountThread; i++)
{
// 创建指定数量的线程
// 是线程调用Run方法
// 启动线程
Thread trd = new Thread(new ThreadStart(Run));
trd.Name = "线程" + i;
trd.Start();
}
} public static void Run()
{ muxConsole.WaitOne(); //阻塞队列
Interlocked.Increment(ref poolFlag); //标记+1
if (poolFlag != maxThread) //判断是否等于上限
muxConsole.ReleaseMutex(); //如果此线程达不到可执行线程上限,则继续开通,让后面的线程进来
Console.WriteLine("{0} 正在运行....../n", Thread.CurrentThread.Name);
Thread.Sleep(5000); //模拟执行
Console.WriteLine("{0} 已经中止....../n", Thread.CurrentThread.Name); //标记-1
Interlocked.Decrement(ref poolFlag);
}
LinkedList<int> lstTask=new LinkedList<int>;
for(int i=0;i<99999;i++)
{
lstTask.AddLast(i);
}...//创建3个线程
for(i=0;i<3;i++)
{
CreateThread();
}....public void CreateThread()
{
Thread h=new Thread(test);
}public void test()
{
int nData;
lock(lstTask)
{
if (lstTask.Count==0) return;
nData=lstTask.First.Value;
lstTask.RemoveFirst();
} ...//对nData处理
}