- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
The Thread Pool in Visual C#
The Thread Pool Draw QR-Code In Visual C#.NET Using Barcode drawer for VS .NET Control to generate, create QR Code image in .NET framework applications. www.OnBarcode.comScan QR Code 2d Barcode In C#.NET Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comThe .NET Framework provides a thread pool, which is a collection of worker threads available to perform short pieces of work. The thread pool continuously adjusts the number of threads that are allowed to process work items simultaneously in an attempt to optimize throughput. The exact algorithm used to adjust the thread count is not documented, but as a general rule, if the system is not busy, work will typically be serviced very quickly after you queue it up. But as the computer becomes busier, items will sit in the queue for longer the thread pool tries to avoid the overheads of preemption, thread switching, and resource contention by not running too much concurrent work. When a system is already busy, trying to process more work items would probably slow it down further, and so Denso QR Bar Code Creator In C# Using Barcode generation for VS .NET Control to generate, create Denso QR Bar Code image in .NET framework applications. www.OnBarcode.comBarcode Drawer In C# Using Barcode drawer for Visual Studio .NET Control to generate, create Barcode image in .NET applications. www.OnBarcode.comkeeping items queued up and processing fewer at a time is likely to result in better overall performance. The simplest way to use the thread pool is through its QueueUserWorkItem method. Example 16-6 shows a modification to the previous examples rather than creating threads, it uses the thread pool. QueueUserWorkItem takes a delegate to any method that accepts a single object as its argument, so it s happy with the same Go method as Example 16-2. (Unlike the Thread constructor, there s no overload that accepts a method without an argument the thread pool insists on there being an argument whether you have a use for one or not.) UPC-A Supplement 5 Encoder In C#.NET Using Barcode encoder for .NET framework Control to generate, create UCC - 12 image in .NET applications. www.OnBarcode.comEAN / UCC - 14 Creation In Visual C# Using Barcode drawer for VS .NET Control to generate, create UCC.EAN - 128 image in VS .NET applications. www.OnBarcode.comstatic void Main(string[] args) { ThreadPool.QueueUserWorkItem(Go, "One"); ThreadPool.QueueUserWorkItem(Go, "Two"); Go("Main"); // Problem: not waiting for work items to complete! USS Code 39 Creator In Visual C# Using Barcode encoder for Visual Studio .NET Control to generate, create Code 3/9 image in .NET applications. www.OnBarcode.comMaking Planet In C# Using Barcode drawer for VS .NET Control to generate, create Planet image in .NET framework applications. www.OnBarcode.com} QR-Code Printer In Visual Basic .NET Using Barcode printer for .NET framework Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications. www.OnBarcode.comQuick Response Code Generation In None Using Barcode drawer for Font Control to generate, create QR-Code image in Font applications. www.OnBarcode.comThis example has a problem: if the main thread finishes first, the program may exit before the thread pool work items complete. So this only illustrates how to start work on the thread pool. This might not be a problem in practice it depends on your application s typical life cycle, but you may need to add additional code to coordinate completion. Running on a quad-core machine, this particular example behaves in much the same way as the previous ones, because the thread pool ends up creating a thread for both work items. On a single-core machine, you might see a difference it could decide to let the first item run to completion and then run the second afterward. The thread pool is designed for fairly short pieces of work. One of the most important jobs it was originally introduced for was to handle web requests in ASP.NET web applications, so if you re wondering how much work constitutes fairly short, a reasonable answer is about as much work as it takes to generate a web page. .NET 4 introduces a new way to use the thread pool, the Task Parallel Library, which offers a couple of advantages. First, it handles certain common scenarios more efficiently than QueueUserWorkItem. Second, it offers more functionality. For example, tasks have much more comprehensive support for handling errors and completion, issues Example 16-6 utterly fails to address. If the main thread finishes before either of the work items is complete, that example will simply exit without waiting for them! And if you want the main thread to discover exceptions that occurred on the thread pool threads, there s no easy way to do that. If any of these things is important to you, the Task Parallel Library is a better way to use the thread pool. There s a whole section on that later in this chapter. For now, we ll continue looking at some aspects of threading that you need to know, no matter what multithreading mechanisms you may be using. Make UCC - 12 In None Using Barcode maker for Word Control to generate, create UPC-A Supplement 5 image in Word applications. www.OnBarcode.comGTIN - 13 Drawer In Objective-C Using Barcode creator for iPhone Control to generate, create EAN-13 image in iPhone applications. www.OnBarcode.comPrinting Barcode In Objective-C Using Barcode maker for iPad Control to generate, create Barcode image in iPad applications. www.OnBarcode.comBarcode Encoder In Java Using Barcode encoder for Android Control to generate, create Barcode image in Android applications. www.OnBarcode.comGenerating ANSI/AIM Code 39 In VS .NET Using Barcode creator for Reporting Service Control to generate, create USS Code 39 image in Reporting Service applications. www.OnBarcode.comGenerating Code 128 Code Set B In None Using Barcode printer for Software Control to generate, create Code 128 Code Set A image in Software applications. www.OnBarcode.comPrint GS1 - 12 In Java Using Barcode maker for Java Control to generate, create GTIN - 12 image in Java applications. www.OnBarcode.comUPC A Reader In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comMaking Code-128 In Visual Studio .NET Using Barcode creator for Reporting Service Control to generate, create Code 128B image in Reporting Service applications. www.OnBarcode.comBarcode Scanner In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.com |
|