Multithreading: final thoughts on the thread pool

Published Fri, Jun 5 2009 12:36

Now that we’ve ended our study of the thread pool, you know that we’ve got two ways of spanning new threads for performing work in parallel: we can manage the thread lifetime by using the Thread class directly or we can rely on thread pool for managing threads for us. Since there are options, you might be wondering which option you should choose for your code.

Once more, there’s really no definitive answer: it all depends on your scenario. If I had to give an answer to that, I’d say that most of the time you’d be better using the thread pool instead of creating your own threads. There are, however, a couple of scenarios where you should create your threads explicitly. For instance:

  • if you need to set the apartment type;
  • if you need to set the type of thread (pool threads are always background threads);
  • if the application will take a long time to end.

Btw, there are also some scenarios where you should be careful in relying on the thread pool. For instance, in ASP.NET apps, you shouldn’t forget that ASP.NET will also use it for its own work (everything should go well if you’re just queuing small tasks: just don’t get too greedy and start queuing work on the thread pool:))

For those that are worried with performance, I guess that using the pool will improve it for many scenarios (specially if we’re talking about parallelizing small tasks) because you’re amortizing the thread creation by using the same threads several times for different tasks.

The new .NET 4.0 thread pool introduces several new features which you can reuse when using the parallel library. This is really cool stuff and I’ll redirect you to Daniel Moth’s excellent blog for getting more details on this new feature.

And that’s it for now. In the next posts, we’ll keep looking at multithreaded

Filed under:

Comments

# Eric Hauser said on Friday, June 05, 2009 10:45 AM

Luis,

I have not had a chance to look at the .NET 4.0 stuff yet, but have they introduced a wrapper around the ThreadPool that would let you process tasks using a predefined amount of threads?  There are a lot of times you want to process a task in the thread pool, but you want to constrain the amount of threads that are being used to process a given task.  This is a pretty common scenario that you can do yourself, but it seems like something that should ship in the framework -- ala Java's Executors and ExecutorService.

# 9eFish said on Sunday, June 07, 2009 8:22 PM

9efish.感谢你的文章 - Trackback from 9eFish

# luisabreu said on Monday, June 08, 2009 3:38 PM

Eric, I still didn't have the time to take a deep look at the 4.0 version. Daniel Moth has some info on this stuff, but I haven't looked at it yet...

# hope said on Thursday, November 26, 2009 5:46 PM

Hi everyone

Sorry to be off topic but I coudn't resist sharing this, hoping it might help some of you.

I am not really new to this forum but never bothered to write anything until now. I have reading around this and that forum to find some solution to my problems. I don't want to bore you guys with my horror story but I would like to say a few words to encourage others.

I use to get panic attacks since my childhood and now I am 26. I was some how living with it due to support from my father and friend. I am shy by nature and find it hard to mix with people and so I have few friends. But around 5 years ago, things turned to worst. I lost my parents in an accident. I then my life was turned upside down. I some how managed to get a boyfriend and I finally felt like coming back to life. I loved her and he too but I guess, I was making his life miserable and so he left, 6 months ago. Ok enough of my sad life.

I actually never lost faith in myself. I was taking medication and I spent quite a lot of time on internet looking for some miracle information. I bought lot of books, CDs and what not. And then, I stumbled upon this course by Mr. Sal. It's called Panic away. Just search for "panicaway dot com" and u will find it. I also tried the famous linden method but former seems to work better for me.

And since then my life is slowly and slowly turning around.

His approach is simple and yet effective. I started reading it around 4 months ago and the last attack that I got was 41 days ago. Earlier, it used to be like 1-2 times each week.I wish that I had found this course an year ago. Anyway. That's it, I would say that you guys so give it a try and if this helps you then my effort of writing this looong post would be worthwhile.

I am now looking forward to a life without fears. Wish me good luck.

[img]trend-fusion.com/.../smiley4.gif[/img]

[img]trend-fusion.com/.../smiley7.gif[/img]

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover