C \ # 다 중 스 레 드 --- < 1 >

11677 단어
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;

namespace ConsoleApplication1
{
    //
    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();
            }
        }
    }

    class Program
    {
        static void 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();
        }
    }
}

좋은 웹페이지 즐겨찾기