Regardless of the number of processors, excess threads affect performance. System.Threading.ThreadPool manages threads, including the reuse of expired threads, based on resources. Accessing threads in ThreadPool is similar to explicit use of the THRead class. However, as Listing 15.5 shows, THReadPool has the advantage of passing parameters even in the 1.0 implementation, where ParameterizedThreadStart is unavailable. Listing 15.5. Using ThreadPool Instead of Instantiating Threads Explicitly using System; using System.Threading; public class ThreadPools { public const int Repetitions = 1000; public static void Main() { ThreadPool.QueueUserWorkItem(DoWork, '.'); for (int count = 0; count < Repetitions; count++) { Console.Write('-'); } // Pause until the thread completes Thread.Sleep(1000); } public static void DoWork(object state) { for (int count = 0; count < Repetitions; count++) { Console.Write(state); } } } | The results of Listing 15.5 appear in Output 15.4. Output 15.4 shows the same intermingling of . and - as Output 15.3 does. The thread pool class does not return a handle to the thread itself, which prevents the calling thread from controlling it with the thread management functions described earlier in the chapter, or easily monitoring its state to determine when it completes. Assuming these deficiencies are not critical, developers should consider using the thread pool over explicit thread creation. This provides more-efficient execution on single- and multiprocessor computers as well as a simple means for passing arbitrary data, even within the 1.0 framework. Output 15.4. ...................................------------------------------------- ------------------------------------------------------------------------ ------------------------------------------------------------------------ ------------------------------------------------------------------------ ------------------------------------------------------------------------ ----------------------------------------------------------------------.. ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ .................................--------------------------------------- ------------------------------------------------------------------------ ------------------------------------------------------------------------ ------------------------------------------------------------------------ ------------------------------------------------------------------------ --------------------------------------------------------------------.... ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ...............................----------------------------------------- ------------------------------------------------------------------------ ------------------------------------------------------------------------ -------------------------............................................... ........................................................................ ........................................................ | Unfortunately, however, thread pool use is not without its pitfalls. Activities like I/O operations and other framework methods that internally use the thread pool can consume threads as well. Consuming all threads within the pool can delay execution and, in extreme cases, cause a deadlock. |