ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta), new SomeState(0));

348 篇文章 0 订阅
67 篇文章 0 订阅

using System;
using System.Collections;
using System.Threading;

namespace ThreadExample
{
    /// <summary>
    /// 这是用来保存信息的数据结构,将作为参数被传递
    /// </summary>
    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;
        }

        /// <summary>
        /// 线程池里的线程将调用 Beta()方法
        /// </summary>
        /// <param name="state"></param>
        public void Beta(Object state)
        {
            // 输出当前线程的 hash 编码值和 Cookie 的值
            Console.WriteLine(" {0} {1} :", Thread.CurrentThread.GetHashCode(), ((SomeState)state).Cookie);
            Console.WriteLine("HashCount.Count=={0}, Thread.CurrentThread.GetHash Code()=={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;
            }

            Thread.Sleep(2000);
            // Interlocked.Increment() 操作是一个原子操作,具体请看下面说明
            Interlocked.Increment(ref iCount);
            if (iCount == iMaxCount)
            {
                Console.WriteLine();
                Console.WriteLine("Setting eventX ");
                eventX.Set();
            }
        }
    }

    public class SimplePool
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Thread Pool Sample:");

            bool W2K = false;

            // 允许线程池中运行最多 10 个线程
            int MaxCount = 10;

            // 新建 ManualResetEvent 对象并且初始化为无信号状态
            ManualResetEvent eventX = new ManualResetEvent(false);

            Console.WriteLine("Queuing {0} items to Thread Pool", MaxCount);

            // 注意初始化 oAlpha 对象的 eventX 属性
            Alpha oAlpha = new Alpha(MaxCount);
            oAlpha.eventX = eventX;
            Console.WriteLine("Queue to Thread Pool 0");
            try
            {
                // 将工作项装入线程池
                // 这里要用到 Windows 2000 以上版本才有的 API,所以可能出现 NotSupportedException 异常
                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();
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值