方法一:
Queue<BookInfo> mq = new Queue<BookInfo>();
int threadNums =2;
int spiderCount = 0;
private void sendMsg(int nums)
{ if (mq.Count > 0) //如果有消息就立刻传送出去
{
spiderCount++; //dosth.....
spiderCount--; }
}
protected void BuildTask(int nums)
{ while (true)
{
if (spiderCount < threadNums)
{
for (int i = 0; i < threadNums; i++)
{
Thread th = new Thread(() => sendMsg(i));
th.Start();
} }
}
}方法二:void Run(){
for (int i = 0; i < threadNums; i++)
{
Thread th = new Thread(() => BuildTask(i));
th.Start();
}} Queue<BookInfo> mq = new Queue<BookInfo>();
int threadNums =2;
int spiderCount = 0;
private void sendMsg(int nums)
{
if (mq.Count > 0){
//dosth.....
} }
protected void BuildTask(int nums)
{ while (true)
{
sendMsg(nums);
}
}
上面两种方法,求大侠指证一下,这两个方法的优点和不足,什么情况下用哪种方法更好。感谢
Queue<BookInfo> mq = new Queue<BookInfo>();
int threadNums =2;
int spiderCount = 0;
private void sendMsg(int nums)
{ if (mq.Count > 0) //如果有消息就立刻传送出去
{
spiderCount++; //dosth.....
spiderCount--; }
}
protected void BuildTask(int nums)
{ while (true)
{
if (spiderCount < threadNums)
{
for (int i = 0; i < threadNums; i++)
{
Thread th = new Thread(() => sendMsg(i));
th.Start();
} }
}
}方法二:void Run(){
for (int i = 0; i < threadNums; i++)
{
Thread th = new Thread(() => BuildTask(i));
th.Start();
}} Queue<BookInfo> mq = new Queue<BookInfo>();
int threadNums =2;
int spiderCount = 0;
private void sendMsg(int nums)
{
if (mq.Count > 0){
//dosth.....
} }
protected void BuildTask(int nums)
{ while (true)
{
sendMsg(nums);
}
}
上面两种方法,求大侠指证一下,这两个方法的优点和不足,什么情况下用哪种方法更好。感谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货