Search:
Locator+ Code:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Back to VSLive! Orlando Show Daily Home

email article
printer friendly

Power Threading in the .NET Framework
Use the system thread pool to create more efficient multithreaded applications.
by Jon Rauschenberger

VSLive! Orlando, September 18, 2002

Note: Jon Rauschenberger is presenting "Threading with .NET" on Wednesday, September 18, at VBITS Orlando. This tip is taken from the material for that session.

The .NET Framework exposes power threading tools to all developers—regardless of the language they're using. For the first time, VB developers can easily create multithreaded applications that take full advantage of the Windows platform's threading capabilities. Taking advantage of this functionality is relatively simple. For example, consider this class:

Public Class Worker

    Public Sub DoSomeWork()
        Thread.Sleep(1000)
    End Sub

End Class

This simple class exposes a single method that simulates a long-running piece of work by sleeping the active thread for one second. If you need to call this method multiple times, you will benefit by spawning multiple threads and running those calls on separate threads. All the classes you need to write multithreaded code are in the System.Threading namespace. To use these classes, import that namespace with this code:

Imports System.Threading

If you needed to call the DoSomeWork method 100 times, you could use this code:

        For currentWorkItem = 1 To 100

            Dim currentWorker As New Worker()
            Dim currentThreadStart As New _
               ThreadStart( _
               AddressOf currentWorker.DoSomeWork)
            Dim newThread As New _
               Thread(currentThreadStart)
            newThread.Start()

        Next

This will spawn a new thread for each request and then run the DoSomeWork method in the thread. The problem with this code is that you are spawning 100 threads to process the 100 calls. Spawning more than approximately 25 threads per CPU leads to significant inefficiencies. When multiple threads are executing, the operating system has to schedule processor time for each thread. The more threads you spawn, the more time the OS must spend scheduling CPU time for each thread, and the less time the CPU spends executing the work each thread is doing.

To get around this issue, the .NET Framework provides a system thread pool that you can queue work into. The thread pool is a pool of threads that process requests off an internal work queue. The maximum number of threads in the pool defaults to 25 per CPU. This means that regardless of how many requests you drop on the work queue, there will never be more threads spawned than the number of CPUs times 25. To use the system thread pool to call the DoSomeWork method 100 times, you use the ThreadPool.QueueUserWorkItem method. This method takes a delegate as an input parameter. When the item is pulled off the queue by one of the worker threads, the thread will call the delegate and your code will execute.

Back to top




Sponsored Links
IBM Rational Webcast: What is your code doing?
Learn how XDE can jumpstart your development efforts.

Learn about Code Sharing and Reuse.
Download our FREE White Paper now.

Code at the Speed of Thought with Developer Express Technologies

Get Crystal Reports 9
And Get Free Software!

Delivering Better Software, Faster for the Microsoft .NET Framework

Sonic Stylus Studio 5.0—XQuery, XML, XSTL
For a FREE evaluation copy, click here

Tools Offer:
Try Microsoft Visual Studio Tools
for the Microsoft Office System, FREE.

Visual Studio .NET
New version 2003

Microsoft Windows Server 2003. Try the new platform.

Need custom e-forms, datagrids and database-connected apps? Free Eval.

Sponsored Whitepapers
Office 2003 Offers Expanded XML Integration

Use .NET and XML to Power New Office Solutions



Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home