ThreadPool 简单的用法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadPoolDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int nWorkerThreads;
            int nCompletionPorteads;
            ThreadPool.GetMaxThreads(out nWorkerThreads, out nCompletionPorteads);
            Console.WriteLine("Max worker threads:{0} Completion Threads:{1}", nWorkerThreads, nCompletionPorteads);

           for(int i=0;i<5;i++)
           {
               ThreadPool.QueueUserWorkItem(JobForAthread);
           }
           Thread.Sleep(3000);
        }

        static void JobForAthread(object state)
        {
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("Loop {0},Running inside pooled thread{1}",i, Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(50);
            }
        }
    }
}

posted @ 2012-09-24 17:01  在 水 一 方  阅读(504)  评论(0编辑  收藏  举报