Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Reducing Computing Time with Multithreading

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.

Figure 1: In the multithreading architecture shown here, StartWork method will instantiate different engine at the same time to execute multiple request at a single time.

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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.