Visual Basic 2005 for Programmers (2nd Edition)

15.1. Introduction

It would be nice if we could perform one action at a time and perform it well, but that is usually difficult to do. The human body performs a great variety of operations in parallelor, as we will say throughout this chapter, concurrently. Respiration, blood circulation, digestion, walking and talking, for example, can occur concurrently. All the senses sight, touch, smell, taste and hearingcan be employed at once. Computers, too, perform operations concurrentlycompiling a program, sending a file to a printer and receiving electronic mail messages can all occur in parallel.

Ironically, most programming languages do not enable programmers to specify concurrent activities. Rather, programming languages generally provide only a simple set of control statements that enable programmers to perform one action at a time, proceeding to the next action after the previous one has finished. Historically, the type of concurrency that computers perform today has generally been implemented as operating-system "primitives" available only to highly experienced "systems programmers."

The Ada programming language, developed by the United States Department of Defense, made concurrency primitives widely available to defense contractors building military command-and-control systems. However, Ada has not been widely used in academia and commercial industry.

The FCL provides concurrency primitives. You specify that applications contain "threads of execution," each of which designates a portion of a program that may execute concurrently with other threadsthis capability is called multithreading. Multithreading is available to all .NET programming languages, including Visual Basic, Visual C# and Visual C++. The FCL's multithreading capabilities are located in the System.Threading namespace.

Performance Tip 15.1

A problem with single-threaded applications is that lengthy activities must complete before other activities can begin. In a multithreaded application, threads can be distributed across multiple processors (if they are available) so that multiple tasks are performed concurrently, allowing the application to operate more efficiently. Multithreading can also increase performance on single-processor systems that simulate concurrencywhen one thread cannot proceed, another can use the processor.

This chapter discusses many applications of concurrent programming. When programs download large files, such as audio clips or video clips over the Internet, users do not want to wait until an entire clip downloads before starting the playback. To solve this problem, we can put multiple threads to workone thread downloads a clip, while another plays the clip. These activities proceed concurrently. To avoid choppy playback, we synchronize the threads so that the player thread does not begin until there is a sufficient amount of the clip in memory to keep the player thread busy.

Another example of multithreading is the CLR's automatic garbage collection. C and C++ require programmers to explicitly reclaim dynamically allocated memory. The CLR's garbage-collector thread reclaims dynamically allocated memory that is no longer needed.

Good Programming Practice 15.1

Set an object reference to Nothing when the program no longer needs that object. This enables the garbage collector to determine at the earliest possible moment that the object can be garbage collected. If such an object has other references to it, that object cannot be collected. Note that you still cannot predict when, or if, the garbage collector will actually reclaim the memory.

Writing multithreaded programs can be tricky. Although the human mind can perform functions concurrently, people find it difficult to jump between parallel "trains of thought." To see why multithreading can be difficult to program and understand, try the following experiment: Open three books to page 1 and try reading the books concurrently. Read a few words from the first book, then read a few words from the second book, then read a few words from the third book, then loop back and read the next few words from the first book, etc. After this experiment, you will appreciate the challenges of multithreadingswitching between books, reading briefly, remembering your place in each book, moving the book you are reading closer so you can see it, pushing aside books you are not reading and, amid all this chaos, trying to comprehend the content of the books!

Категории