如何应用CLR线程池来管理多线程

    class Program
    {
        static void Main(string[] args)
        {
            int intWorkerThreas;    //定义最大工作线程数
            int intCompletionPortThreads;   //定义最大I/O线程数
            ThreadPool.GetMaxThreads(out intWorkerThreas, out intCompletionPortThreads);  //获取最大线程数信息
            Console.WriteLine("最大工作线程数:{0},最大I/O线程数:{1}",intWorkerThreas,intCompletionPortThreads);
            for (int i = 0; i < 3; i++)//从线程池中申请3个线程
            {
                ThreadPool.QueueUserWorkItem(TestThreadPool);//在线程池中申请使用线程
            }
            Console.Read();
        }
        static void TestThreadPool(object obj)
        {
            //输出正在线程池中当前正在运行的线程信息
            Console.WriteLine("线程池中,正在运行的线程的唯一标识为{0}", Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(100);//休眠100毫秒
        }
    }
posted @ 2015-10-26 17:00  code、sky  阅读(218)  评论(0编辑  收藏  举报