c# 线程集合中怎么轮流开启一定数量线程??

2025-05-17 15:30:34
推荐回答(1个)
回答1:

自己写个简单线程池,我在这里随便写的点代码不确定编译通过
class threadinfo
{
public int status;// 1,正在运行,0,未运行,-1运行完成
public thread th;
}
class threadManager
{
List _lst = new List();
public int RunNumber{get;set;}
public addThread(Thread th){_lst.add(th);}
// 线程结束的回调
publc threadCallback()
{
foreach(var info in _lst)
{
if(Thread.CurrentThread==info.th)
{
info.status = -1;
start();
break;
}
}
}
// 运行线程
public start()
{
int run = 0;
foreach(var info in _lst)
{
if(run>=RunNumber) break;
if(info.status == 1) run++;
else if(info.status ==0)
{
info.th.start();
info.status = 1;
run++;
}
}
}
}

只是大概这么个意思,你自己结合实际情况弄吧