Visual Basic Concepts

Using Multiple Threads of Execution

See Also

Multithreading on a single-processor machine may not provide quite the results you expect. Consider, for example, the two methods shown in Figure 8.8.

Figure 8.8   Multithreaded method calls may appear to be slower

Method A and method B are called at the same time. In a single-threaded component, the requests are serialized, so that B doesn’t begin until A has finished. With multithreading, the two active threads duel for the processor’s attention.

Not only does the perceived average completion time increase, but more processor time is spent switching between threads.

The problem is that method A and method B take about the same amount of time. On a single-processor machine, multithreading results in a perceived performance improvement only with a mix of long and short tasks.

For example, if method B required only three time slices to complete, the user of the system would perceive a huge improvement in the responsiveness of method B — and only a slight degradation in the time required to execute method A.

The scenarios in which multithreading shows to best advantage are those in which most threads spend a substantial percentage of the time blocked — for example, waiting for file I/O — so that only one or two threads are actively executing code at any given time.

For More Information   Multithreading can be enabled in components marked for unattended execution (that is, having no user interface), as described in "Scalability and Multithreading."