Dot Net Thread Pool
Multi threading is a non-disputed technique for the individual wanting to become a Dot Net master. I have even seen people doing threading for a Microsoft Word Add-in. Historically it was often used to provide the user with indications of how much work had been done, and how much work remained, on long running tasks.
Today, with multi-core machines, you can actually do real work in parallel with multiple threads. Why would you use a Thread Pool? Because there is overhead associated with starting up a thread. If the threads are already spun up and waiting in a pool, you can later assign work to those threads without the additional overhead of creating a thread instance.
We use the pooling concept in ADO.Net Connection Pooling. The difference is that connection pooling only works with data source connections. The MS Thread Pool works with threads you create.
Threads are a pretty easy concept on the whole. But, managing and causing threads to communicate is another matter altogether. Microsoft has put together a number of threading tools easing the work of developers by having generic reusable frameworks.
One of the tools I use is the Dot Net ThreadPool class. The Thread Pool is a global object inside each Dot Net process. Each process may have only one Thread Pool. That being said, all threads you create to run in the Thread Pool share the resources of the Thread Pool with other processes, even if you don’t write them.
There is a limitation to the number of threads you may have in a Thread Pool, and there is also a limitation to the number of active threads you may have in a Thread Pool. In short, you may have many more instances of a thread in the Thread Pool than are actively operating. You generally have no way of knowing what thread your object will operate on, when it will begin and when it will end.
The key thing to be aware of when using a Thread Pool out of the box is that if you have a thread executing a long running task, that thread will not become available in the thread pool until it completes. So, if you have some work that is of higher priority, or requires certain characteristics that the Thread Pool class manages on your behalf, you may need to look elsewhere, or build your own techniques on top of the Thread Pool class.
I found helpful information on Stack Overflow, MSDN, MSDN Magazine as well as other locations.
It doesn’t hurt to try and write your own Thread Pool classes. That certainly will emphasize to you how nice the Dot Net ThreadPool classes are.
Do you have your own Threading practices? You can share them by sending an email to btaylor@sswug.org.
Cheers,
Ben
$$SWYNK$$
Featured White Paper(s)
Top 10 Tips for Optimizing SQL Server Performance
read more)
Featured Script
dba3_0070_hr_Set_InsertNewSet_Article
Creates stored procedure hr_Set_InsertNewSet Used in demonstrating manipulations of a Maps and Sets logical design of a hiera… (read more)