Thread Pooling

Thread Pooling

I ll close this chapter by briefly mentioning a handy System.Threading class named ThreadPool that provides a managed thread pooling API. The basic idea behind thread pooling is that instead of launching threads yourself, you pass requests to a thread pool manager. The thread pool manager maintains a pool of threads that s sized as needed to satisfy incoming requests. The following example demonstrates a very simple use of the ThreadPool class:

using System; using System.Threading; class MyApp { static int count = 0; static void Main () { WaitCallback callback = new WaitCallback (ProcessRequest); ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); ThreadPool.QueueUserWorkItem (callback); Thread.Sleep (5000); // Give the requests a chance to execute } static void ProcessRequest (object state) { int n = Interlocked.Increment (ref count); Console.WriteLine (n); } }

This application counts from 1 to 10 in a console window and uses background threads to do the writing. Rather than launch 10 threads itself, the application submits 10 requests to ThreadPool by calling ThreadPool.QueueUserWorkItem. Then ThreadPool determines how many threads are needed to handle the requests.

You can use an alternate form of QueueUserWorkItem one that accepts an Object in its second parameter to pass additional information in each request. The following example passes an array of five integers:

int[] vals = new int[5] { 1, 2, 3, 4, 5 }; ThreadPool.QueueUserWorkItem (callback, vals);

QueueUserWorkItem s second parameter is the first (and only) parameter to the callback method:

static void ProcessRequest (object state) { int[] vals = (int[]) state; ... }

A simple cast converts the Object reference to a strong type and gives the callback method access to the data provided by the requestor.

You must never terminate a pooled thread. The thread pool manager creates pooled threads, and it terminates them, too. You can use Thread s IsThreadPoolThread property to determine whether a thread is a pooled thread. In the following example, the current thread terminates itself if and only if it s a nonpooled thread:

if (!Thread.CurrentThread.IsThreadPoolThread) Thread.CurrentThread.Abort ();

For server applications anticipating a high volume of requests that require concurrent processing, thread pooling simplifies thread management and increases performance. The performance increase comes because the thread pool manager maintains a pool of threads that it can use to service requests. It s far faster to transfer a call to an existing thread than it is to launch a whole new thread from scratch. Plus, divvying up requests among threads enables the system to take advantage of multiple CPUs if they re present. You do the easy part by handing requests off to ThreadPool. ThreadPool does the rest.



Programming Microsoft  .NET
Applied MicrosoftNET Framework Programming in Microsoft Visual BasicNET
ISBN: B000MUD834
EAN: N/A
Year: 2002
Pages: 101

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net