关于线程池的问题,求助!

xinuaile 2004-04-19 07:27:07
关于线程池的问题,求助! 注释掉Thread.Sleep(1000);后有时显示hello,有时无,

为什么? 为什么生成对象可以static AutoResetEvent Event1 = new AutoResetEvent(false); static加在类前面?

using System;
using System.Threading;
public class Example {
public static void Main() {
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

Console.WriteLine("Main thread does some work, then sleeps.");

// Thread.Sleep(1000);

Console.WriteLine("Main thread exits.");
}

// This thread procedure performs the task.
static void ThreadProc(Object stateInfo) {

Console.WriteLine("Hello from the thread pool.");
}
}
==============================
//为什么执行完 if (W2K)就跳转到Beta(Object state)的Console.WriteLine方法了?我使用了F11跟踪了半天,也没有看明白,请讲述工作原理!

using System;
using System.Collections;
using System.Threading;
namespace Test
{
//这是用来保存信息的数据结构,将作为参数被传递
public class SomeState

{
public int Cookie;
public SomeState(int iCookie)
{
Cookie = iCookie;
}
}

public class Alpha

{
public Hashtable HashCount;
public ManualResetEvent eventX;
public static int iCount = 0;
public static int iMaxCount = 0;
public Alpha(int MaxCount)
{
HashCount = new Hashtable(MaxCount);
iMaxCount = MaxCount;
}

// 线程池里的线程将调用Beta()方法
public void Beta(Object state)
{
///输出当前线程的hash编码值和Cookie的值
Console.WriteLine(" {0} {1} :", Thread.CurrentThread.GetHashCode(),
((SomeState)state).Cookie);
Console.WriteLine("HashCount.Count=={0}, Thread.CurrentThread.GetHashCode()=={1}", HashCount.Count, Thread.CurrentThread.GetHashCode());
lock (HashCount)

{
// 如果当前的Hash表中没有当前线程的Hash值,则添加之
if (!HashCount.ContainsKey(Thread.CurrentThread.GetHashCode()))
HashCount.Add (Thread.CurrentThread.GetHashCode(), 0);
HashCount[Thread.CurrentThread.GetHashCode()] =
((int)HashCount[Thread.CurrentThread.GetHashCode()])+1;
}

int iX = 2000;
Thread.Sleep(iX);
//Interlocked.Increment()操作是一个原子操作,具体请看下面说明
Interlocked.Increment(ref iCount);
if (iCount == iMaxCount)

{
Console.WriteLine();
Console.WriteLine("Setting eventX ");
eventX.Set();
}
}
}

public class SimplePool

{
public static int Main(string[] args)
{
Console.WriteLine("Thread Pool Sample:");
bool W2K = false;
int MaxCount = 10;//允许线程池中运行最多10个线程
//新建ManualResetEvent对象并且初始化为无信号状态
ManualResetEvent eventX = new ManualResetEvent(false);
Console.WriteLine("Queuing {0} items to Thread Pool", MaxCount);
Alpha oAlpha = new Alpha(MaxCount); // 创建工作项
//注意初始化oAlpha对象的eventX属性
oAlpha.eventX = eventX;
Console.WriteLine("Queue to Thread Pool 0");
try

{
// 将工作项装入线程池
// 这里要用到Windows 2000以上版本才有的API,所以可能出现NotSupportException异常

ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta),
new SomeState(0));
W2K = true;
}

catch (NotSupportedException)

{
Console.WriteLine("These API's may fail when called on a non-Windows 2000 system.");
W2K = false;
}
if (W2K)//如果当前系统支持ThreadPool的方法.

{
for (int iItem=1;iItem < MaxCount;iItem++)
{
//插入队列元素
Console.WriteLine("Queue to Thread Pool {0}", iItem);
ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta),new SomeState(iItem));
}
Console.WriteLine("Waiting for Thread Pool to drain");
// 等待事件的完成,即线程调用ManualResetEvent.Set()方法
eventX.WaitOne(Timeout.Infinite,true);
// WaitOne()方法使调用它的线程等待直到eventX.Set()方法被调用
Console.WriteLine("Thread Pool has been drained (Event fired)");
Console.WriteLine();
Console.WriteLine("Load across threads");
foreach(object o in oAlpha.HashCount.Keys)
Console.WriteLine("{0} {1}", o, oAlpha.HashCount[o]);
}
Console.ReadLine();
return 0;

}
}
}
...全文
64 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
elite2018 2004-04-19
  • 打赏
  • 举报
回复
If you comment out the Sleep, the main thread exits before
the thread pool task runs. The thread pool uses background
threads, which do not keep the application running.

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧