views:

226

answers:

3

In ASP.NET we use async methods because the Thread Pool has limited slots, and by using an async method you free a Thread Pool slot while the method is outbound, increasing the number of requests that a server can handle in the same interval of time.

I guess that in other types of applications (i.e. Windows Forms) the thread limit is much higher. In such cases which is the purpose of async methods when you have both versions (sync and async)?

+3  A: 

You can increase the worker thread pool in the ASP.NET configuration and I have found it to help quite a bit on multi core boxes and IIS 6. From the very little I have done with IIS 7 it seems to manage threads much better without needing to tweak.

The more direct question of why to use both ... you can Async a unit of work so that you free a thread from the pool for another unit of work to begin without waiting for the other thread to complete. Which allows the app to parralize more work at one time.

All your aspx requests are being executed in a Worker Thread anyways, but if you async chunks of your request it can execute parts of it and return the thread back to the pool rather than executing everything in your request all at once.

It is tricky and difficult to master so be careful. :)

http://www.guidanceshare.com/wiki/ASP.NET_2.0_Performance_Guidelines_-_Threading

Outside of ASP, with windows forms there is no such thing as "limitless" thread pools. When you spin up more threads than the box/os can handle you'll start context switching so often that you will be defeating the purpose of threading. I would read further on the subject since it is a very large and advanced topic.

http://en.wikipedia.org/wiki/Thread_(computer_science)

Chad Grant
Okay, but my question was about a scenario, outside the ASP.NET world, where the thread limit was much higher (almost no limit)
Jader Dias
I answered before you edited so I responded to the ASP.NET question. Outside the asp world the same concepts are still relevant and there is no such thing as "no limit" to the amount of threads.
Chad Grant
thanks by the explanation
Jader Dias
+2  A: 

Basically, it's a waste to have threads waiting around doing nothing. It's not a question of limited thread pool or how big you can make it. It's a general desire to have each and every thread used to its maximum capacity. It makes no sense to have a thread go out an wait on a network call or perhaps a long bit of I/O when it could be doing other useful things. This desire is quantified in concepts like scalability, performance and throughput, but that is not necessarily the net goal. The net goal is to use scarce resources (the processors) for as many tasks as possible. An interesting counterpoint to this is the notion of parallelization. The goal there is to properly distribute tasks when you have a large set of resources and not necessarily as many tasks.

JP Alioto
+2  A: 

The purpose of asynchronous methods is to provide a more responsive application. If you run all tasks on the same thread, the application will appear to stop every time your thread has to do a long running synchronous operation because your thread is waiting for the operation to finish and thus is unable to do anything else. To get around this, you can fork off these tasks to separate threads with asynchronous calls.

Brian Rasmussen