Daniele Bochicchio, Stefano Mostarda, and Marco De Sanctis are authors of ASP.NET 4.0 in Practice.
With so many multicore CPUs on the market, multithreading and parallel execution are becoming more popular topics among developers. Both multithreading and parallel execution aim at reducing computing time, providing better performance.
Multithreading is the ability to execute multiple tasks at the same time using different threads (see Figure 1).
Parallel execution is the ability to span a single task across multiple CPUs and use the whole power to execute a computing task in the fastest possible way.
Process, Threads, and Execution
When a program is executed, the operating system creates a particular object called process, giving an isolated memory space to it.
A process contains a specific kind of items called threads, used to execute the code. A process, in fact, does not have the ability to execute anything. A process contains at least one thread (the primary one). When the primary thread is terminated, the process itself is terminated, and the memory is unloaded.
Creating a thread is easier from a performance point of view than creating a process; you are not required to allocate memory.
When a piece of code is executed, the thread is blocked, waiting for the response. If you have a single thread responding to your code execution needs, the problem is simple -- you'll have a waiting list for the code to be executed.
This approach will not work for normal applications. Let's imagine if, while in a production program like the ones in Office, you have to wait for every single operation you'll do before moving on. It will be impossible using a similar approach to have a background spellchecker or start printing while editing a document.
Multithreading is very important; in fact, ASP.NET does support multiple threads. Using this approach, one request does not stop the others and multiple requests at the same time can be served. What is really important at this point is the ability to create new threads and assign a specific code to them to execute part of the work in a different thread. To be clear, I'm speaking of generating multiple threads from a single request to increase response time.
This approach is very useful in scenarios where you need to make calls to external resources, just like databases or web services are.
There is a strong debate about whether generating multiple threads in a web application is a best practice or not because the working threads are shared by all requests. In such a situation, if you can afford a better application componentization, this can be achieved by simply moving the thread generation to a different layer and using the application as a controller and display only. Anyway, the technique shown in the next example may be useful in a lot of scenarios where this componentization is not needed or possible.


