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.

604 Multithreading Chapter 14<br />

from the shared buffer and then displays the data. We display in the program’s output the<br />

values that the producer writes (produces) and that the consumer reads (consumes).<br />

Figure 14.8 demonstrates a producer and a consumer accessing a single shared cell<br />

(Integer variable mBuffer, Fig. 14.5 line 9) of memory without any synchronization.<br />

Both the consumer and the producer threads access this single cell: The producer thread<br />

writes <strong>to</strong> the cell, whereas the consumer thread reads from it. We would like each value that<br />

the producer thread writes <strong>to</strong> the shared cell <strong>to</strong> be consumed exactly once by the consumer<br />

thread. <strong>How</strong>ever, the threads in this example are not synchronized. Therefore, data can be<br />

lost if the producer places new data in<strong>to</strong> the slot before the consumer consumes the previous<br />

data. In addition, data can be incorrectly repeated if the consumer consumes data again<br />

before the producer produces the next item. To illustrate these possibilities, the consumer<br />

thread in the following example keeps a <strong>to</strong>tal of all the values it reads. The producer thread<br />

produces values from 1 <strong>to</strong> 4. If the consumer reads each value once and only once, the <strong>to</strong>tal<br />

would be 10. <strong>How</strong>ever, if students execute this program several times, they will see that the<br />

<strong>to</strong>tal is rarely, if ever, 10. To emphasize our point, the producer and consumer threads in<br />

the example each sleep for random intervals of up <strong>to</strong> three seconds between performing<br />

their tasks. Thus, we do not know exactly when the producer thread will attempt <strong>to</strong> write a<br />

new value, nor do we know when the consumer thread will attempt <strong>to</strong> read a value.<br />

The program consists of module modSharedCell (Fig. 14.8) and three classes—<br />

CHoldIntegerUnsynchronized (Fig. 14.5), CProducer (Fig. 14.6) and CConsumer<br />

(Fig. 14.7).<br />

Class CHoldIntegerUnsynchronized (Fig. 14.5) consists of instance variable<br />

mBuffer (line 9) and property Buffer (lines 12–28), which provides Get and Set<br />

accessors. Property Buffer’s accessors do not synchronize access <strong>to</strong> instance variable<br />

mBuffer. Note that each accessor uses class Thread’s Shared property Current-<br />

Thread <strong>to</strong> obtain a reference <strong>to</strong> the currently executing thread and then uses that thread’s<br />

property Name <strong>to</strong> obtain the thread’s name.<br />

1 ' Fig. 14.5: HoldIntegerUnsynchronized.vb<br />

2 ' Definition of a shared integer without synchronization mechanisms.<br />

3<br />

4 Imports System.Threading<br />

5<br />

6 Public Class CHoldIntegerUnsynchronized<br />

7<br />

8 ' buffer shared by producer and consumer threads<br />

9 Private mBuffer As Integer = -1<br />

10<br />

11 ' property Buffer<br />

12 Property Buffer() As Integer<br />

13<br />

14 Get<br />

15 Console.WriteLine(Thread.CurrentThread.Name & _<br />

16 " reads " & mBuffer)<br />

17<br />

18 Return mBuffer<br />

19 End Get<br />

Fig. 14.5 Unsynchronized shared Integer buffer (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!