代码如下:
using System;
using System.Collections;
using System.Threading;namespace ThreadExample
{
    //这是用来保存信息的数据结构,将作为参数被传递
    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("Thread ID : {0} , Thread parameter :{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;
          Console.WriteLine("HashTable is updating: {0} {1}",Thread.CurrentThread.GetHashCode(),HashCount[Thread.CurrentThread.GetHashCode()]);
      
          int iX = 2000;
          Thread.Sleep(iX);
          //Interlocked.Increment()操作是一个原子操作,具体请看下面说明
          Console.WriteLine("Begin Add {0} in thread {1}", iCount, Thread.CurrentThread.GetHashCode());
          Interlocked.Increment(ref iCount);
          Console.WriteLine("End Add {0} in thread {1}", iCount, Thread.CurrentThread.GetHashCode());}
          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");
                    Console.WriteLine("HashTable has {0} Items!", oAlpha.HashCount.Count);
                    foreach(object o in oAlpha.HashCount.Keys)
                        Console.WriteLine("{0} {1}", o, oAlpha.HashCount[o]);
                }
                Console.ReadLine();
                return 0;
            }
        }
    }
我在VS2005中运行结果如下:
Thread Pool Sample:
Queuing 10 items to Thread Pool
Queue to Thread Pool 0
Queue to Thread Pool 1
Queue to Thread Pool 2
Thread ID : 3 , Thread parameter :0
HashCount.Count==0, Thread.CurrentThread.GetHashCode()==3
HashTable is updating: 3 1
Queue to Thread Pool 3
Queue to Thread Pool 4
Queue to Thread Pool 5
Queue to Thread Pool 6
Queue to Thread Pool 7
Queue to Thread Pool 8
Queue to Thread Pool 9
Waiting for Thread Pool to drain
Thread ID : 4 , Thread parameter :1
HashCount.Count==1, Thread.CurrentThread.GetHashCode()==4
Thread ID : 5 , Thread parameter :2
HashCount.Count==1, Thread.CurrentThread.GetHashCode()==5
Begin Add 0 in thread 3
End Add 1 in thread 3
Thread ID : 3 , Thread parameter :3
HashCount.Count==1, Thread.CurrentThread.GetHashCode()==3
HashTable is updating: 4 1
Thread ID : 6 , Thread parameter :4
HashCount.Count==2, Thread.CurrentThread.GetHashCode()==6
Thread ID : 7 , Thread parameter :5
HashCount.Count==2, Thread.CurrentThread.GetHashCode()==7
Thread ID : 8 , Thread parameter :6
HashCount.Count==2, Thread.CurrentThread.GetHashCode()==8
Thread ID : 9 , Thread parameter :7
HashCount.Count==2, Thread.CurrentThread.GetHashCode()==9
Begin Add 1 in thread 4
Thread ID : 10 , Thread parameter :8
Thread ID : 11 , Thread parameter :9
HashCount.Count==2, Thread.CurrentThread.GetHashCode()==11
End Add 2 in thread 4
HashTable is updating: 5 1
HashCount.Count==2, Thread.CurrentThread.GetHashCode()==10
Begin Add 2 in thread 5
End Add 3 in thread 5
HashTable is updating: 3 2
Begin Add 3 in thread 3
End Add 4 in thread 3
HashTable is updating: 6 1
Begin Add 4 in thread 6
End Add 5 in thread 6
HashTable is updating: 7 1
Begin Add 5 in thread 7
End Add 6 in thread 7
HashTable is updating: 8 1
Begin Add 6 in thread 8
End Add 7 in thread 8
HashTable is updating: 9 1
Begin Add 7 in thread 9
End Add 8 in thread 9
HashTable is updating: 11 1
Begin Add 8 in thread 11
End Add 9 in thread 11
HashTable is updating: 10 1
Begin Add 9 in thread 10
End Add 10 in thread 10Setting eventX
Thread Pool has been drained (Event fired)Load across threads
HashTable has 9 Items!
11 1
10 1
9 1
8 1
7 1
6 1
5 1
4 1
3 2
/////////////////////////////////////////
我的疑问是每一个线程开始运行时取线程ID时,发现线程ID有重复的,请问这是怎么回事??