Tag Archives: ThreadPool

ThreadPool

ThreadPool

ThreadPool basically maintain “pool” of threads which are waiting for work items. As thread creation is not a cheap process. By default, the thread pool has 25 threads per processor.

We can Queue up method to the ThreadPool using it’s QueueUserWorkItem method. It takes two parameters – a WaitCallback delegate to the method you want to call and the optional object to pass as parameter to the callback method.

     protected void Page_Load(object sender, EventArgs e)  

        {  

            WaitCallback callBack;   

            callBack = new WaitCallback(DoubleMe);  

            ThreadPool.QueueUserWorkItem(callBack, 1);  

            ThreadPool.QueueUserWorkItem(callBack, 2);  

            ThreadPool.QueueUserWorkItem(callBack, 3);  

        }   

        public void DoubleMe(int i)  

        {   

            // Simulation of processing time   

            Thread.Sleep(1000);  

            String result = (2*i).ToString();

            Response.Write(result + “<BR>”);  

        } 

Leave a comment

Filed under C#