30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

628 Multithreading Chapter 14<br />

Fig. 14.16 Producer and consumer threads accessing a circular buffer (part 4 of 4).<br />

In Fig. 14.16, the outputs include the current occupiedBufferCount, the contents<br />

of the buffers and the current writeLocation and readLocation. In the output, the<br />

letters W and R represent the current writeLocation and readLocation, respectively.<br />

Notice that, after the third value is placed in the third element of the buffer, the fourth value<br />

is inserted at the beginning of the array. This produces the circular buffer effect.<br />

SUMMARY<br />

Computers can perform operations concurrently, such as compiling programs, printing files and<br />

receiving electronic mail messages over a network.<br />

<strong>Program</strong>ming languages generally provide only a simple set of control structures that enable programmers<br />

<strong>to</strong> perform one action at a time, proceeding <strong>to</strong> the next action only after the previous one<br />

finishes.<br />

His<strong>to</strong>rically, the type of concurrency that computers perform generally has been implemented as<br />

operating system “primitives” available only <strong>to</strong> highly experienced “systems programmers.”<br />

The .<strong>NET</strong> Framework Class Library makes concurrency primitives available <strong>to</strong> the applications<br />

programmer. The programmer can specify that an application contains threads of execution, where<br />

each thread designates a portion of a program that might execute concurrently with other threads—<br />

this capability is called multithreading.<br />

A thread that was just created is in the Unstarted state. A thread is initialized using the Thread<br />

class’s construc<strong>to</strong>r, which receives a ThreadStart delegate. This delegate specifies the method<br />

that contains the tasks that a thread will perform.<br />

A thread remains in the Unstarted state until the thread’s Start method is called; this causes the<br />

thread <strong>to</strong> enter the Started state (also known as the Ready, or Runnable, state).<br />

A thread in the Started state enters the Running state when the system assigns a processor <strong>to</strong> the<br />

thread. The system assigns the processor <strong>to</strong> the highest-priority Started thread.<br />

A thread enters the S<strong>to</strong>pped (or Dead) state when its ThreadStart delegate completes or terminates.<br />

A thread is forced in<strong>to</strong> the S<strong>to</strong>pped state when its Abort method is called (by itself or<br />

by another thread).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!