Thursday, June 29, 2006

How many 'Threads' you have?

Multithreading represents the ability for the computer to execute several things at once. Like multitasking really, except multitasking generally refers to the ability to run multiple programs at the same time, while multithreading refers to the ability for a single program to execute multiple “things” at the same time.

The need for multithreading ranges from simple necessity to “luxuries” such as
enhancing the user’s experience.


The need for multithreading ranges from simple necessity to “luxuries” such as enhancing the user’s experience. On the need-side, consider the core Windows operating system and how it executes programs. Without multithreading or multitasking, I would not be able to run Word and open the Windows start menu at the same time. However, that is not an aspect that many developers (besides those working at Microsoft perhaps) worry about. What is more interesting, for instance, is Word’s ability to run the spelling checker in the background while I type my article. I do not have to stop writing to run the spelling checker, nor does the spelling checker interrupt my flow of work in any way. I can simply type away on the main thread, and the verification of my input is performed on a secondary (probably lower priority) thread.


The same applies to practically all applications available today. Often, developers tell me they do not believe they need multithreading because they only write business applications. I have to admit that I do not quite follow their argument. Business application can benefit from multithreading in many ways. For instance, a business application loads data. Whenever data operations may take a while, threading is beneficial, because without threading, the application appears to be hung. Windows flags it as “not responding,” which may result in the user being annoyed (at best) or inappropriately terminating the application (at worst). So which data operations could potentially take a while? All of them! Even very fast data operations may encounter scenarios where the database server is too busy to perform the operation speedily. Or perhaps the server is blocking data access due to another ongoing operation. Furthermore, data processing speed also depends on bandwidth. Unknown components such as Internet connections may result in utterly unpredictable performance results. Modern applications need to be able to handle such situations gracefully.