Managing Multiple Threads with a Thread Pool


There are many real-world situations where the use of a thread makes sense but the thread's lifetime is short. For example, an architect might choose to spawn a thread to handle a short-lived socket connection to a remote party. Suppose the application has an average of 5 such connections occurring simultaneously , but each connection lasts an average of only 1 minute. In that case the application could easily spawn over 100 threads per hour , even though it really only needed 5 at a time.

This kind of behavior is wasteful and expensive and can be especially harmful on devices, with their limited resources. Particularly with devices, developers should always be stingy with resource allocation.

The .NET Compact Framework contains a very limited offering of the ThreadPool class, which is designed specifically to mitigate scenarios like the one just described. The ThreadPool class internally maintains a small number of threads and uses them to perform work that you pass in as specific methods to execute. When a method returns, the thread that executed returns to the pool until the ThreadPool class uses it again to execute another method.

Because the ThreadPool class minimizes thread creations, it causes less garbage to build up in the heap. Since there is a much smaller heap available on devices than on desktop computers, and since the slower processors take longer for garbage to build up, the performance gain can be significant.

To set up a thread pool, follow these steps:

  1. Write a method that the ThreadPool will execute. The method must return void and take no parameters.

  2. Create a new instance of the WaitCallback class, passing the name of the method to execute as an argument.

  3. Call the static method ThreadPool.QueueUserWorkItem , and pass the WaitCallback instance into it. The ThreadPool will execute the method by using one of its threads.

The following sample code demonstrates setting up the ThreadPool to execute two methods, SinglesCounter and DecadeCounter . This sample code is derived from the ThreadPoolDemo sample application.

 
 C# m_singlesWaitCallBack = new WaitCallback(SinglesCounter); m_decadeWaitCallBack = new WaitCallback(DecadeCounter); ThreadPool.QueueUserWorkItem(m_singlesWaitCallBack); ThreadPool.QueueUserWorkItem(m_decadeWaitCallBack); VB m_singlesWaitCallBack = New System.Threading.WaitCallback(AddressOf         SinglesCounter) m_decadeWaitCallBack = New System.Threading.WaitCallback(AddressOf         DecadeCounter) System.Threading.ThreadPool.QueueUserWorkItem(m_singlesWaitCallBack) System.Threading.ThreadPool.QueueUserWorkItem(m_decadeWaitCallBack) 

Controlling Threads with the ThreadPoolDemo Application

The ThreadPoolDemo sample application is located in the folder \SampleApplications\ Chapter4 . There are C# and Visual Basic versions.

Like the SimpleThread sample application, ThreadPoolDemo uses two threads to increment two counters. The first counter increments by one, and the other increments by tens. ThreadPoolDemo uses a ThreadPool to assign threads to the methods that increment the counters.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

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