26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

854 Multithreading Chapter 15<br />

sumer. Also, notice that the consumer twice retrieved value –1 (the default value of<br />

sharedInt set at line 5 of Fig. 15.6) before the producer ever assigned 1 <strong>to</strong> the<br />

sharedInt variable. This example clearly demonstrates that access <strong>to</strong> shared data by<br />

concurrent threads must be controlled carefully or a program may produce incorrect results.<br />

To solve the problems of lost data and doubled data in the previous example, we will<br />

synchronize access of the concurrent producer and consumer threads <strong>to</strong> the shared data.<br />

Each method used by a producer or consumer <strong>to</strong> access the shared data is declared with the<br />

synchronized keyword. When a method declared synchronized is running in an<br />

object, the object is locked so no other synchronized method can run in that object at<br />

the same time.<br />

15.7 Producer/Consumer Relationship with Thread<br />

Synchronization<br />

The application in Fig. 15.8 demonstrates a producer and a consumer accessing a shared<br />

cell of memory with synchronization so that the consumer only consumes after the producer<br />

produces a value. Classes ProduceInteger (Fig. 15.8), ConsumeInteger<br />

(Fig. 15.9) and SharedCell (Fig. 15.11) are identical <strong>to</strong> Fig. 15.4, Fig. 15.5 and<br />

Fig. 15.7 except that they use the new class HoldIntegerSynchronized in this example.<br />

Class HoldIntegerSynchronized (Fig. 15.10) contains two instance variables—sharedInt<br />

(line 6) and writeable (line 7). Also, method setSharedInt<br />

(lines 12–39) and method getSharedInt (lines 44–70) are now synchronized<br />

methods. Objects of class HoldIntegerSynchronized have moni<strong>to</strong>rs, because<br />

HoldIntegerSynchronized contains synchronized methods. Instance variable<br />

writeable is known as the moni<strong>to</strong>r’s condition variable—is a boolean used by<br />

methods setSharedInt and getSharedInt of class HoldIntegerSynchronized.<br />

If writeable is true, setSharedInt can place a value in<strong>to</strong> variable<br />

sharedInt, because the variable currently does not contain information. <strong>How</strong>ever, this<br />

means getSharedInt currently cannot read the value of sharedInt. If writeable<br />

is false, getSharedInt can read a value from variable sharedInt because the variable<br />

currently does contain information. <strong>How</strong>ever, this means setSharedInt currently<br />

cannot place a value in<strong>to</strong> sharedInt.<br />

1 // Fig. 15.8: ProduceInteger.java<br />

2 // Definition of threaded class ProduceInteger<br />

3 public class ProduceInteger extends Thread {<br />

4 private HoldIntegerSynchronized sharedObject;<br />

5<br />

6 // initialize ProduceInteger thread object<br />

7 public ProduceInteger( HoldIntegerSynchronized shared )<br />

8 {<br />

9 super( "ProduceInteger" );<br />

10 sharedObject = shared;<br />

11 }<br />

12<br />

Fig. Fig. Fig. 15.8 15.8 15.8 Class ProduceInteger represents the producer in a producer/<br />

consumer relationship (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!