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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15 Multithreading 867<br />

157<br />

158 // place buffer elements in outputBuffer<br />

159 for ( int count = 0; count < buffer.length; count++ )<br />

160 outputBuffer.append(<br />

161 " " + formatNumber.format( buffer[ count ] ) );<br />

162<br />

163 // update Swing GUI component<br />

164 SwingUtilities.invokeLater( new UpdateThread( outputArea,<br />

165 "\tbuffer: " + outputBuffer ) );<br />

166 }<br />

167<br />

168 } // end class HoldIntegerSynchronized<br />

Fig. Fig. 15.15 15.15 15.15 Class HoldIntegerSynchronized moni<strong>to</strong>rs access <strong>to</strong> a shared array<br />

of integers (part 5 of 5).<br />

Method setSharedInt (lines 34–87) performs the same tasks as it did in<br />

Fig. 15.10, with a few modifications. When execution continues at line 55 after the while<br />

loop, setSharedInt places the produced value in the circular buffer at location<br />

writeLocation. Next, readable is set <strong>to</strong> true (line 58), because there is at least one<br />

value in the buffer that the client can read. Lines 61–63 use SwingUtilities method<br />

invokeLater <strong>to</strong> append the value produced and the cell where the value was placed <strong>to</strong><br />

the JTextArea (method run of class UpdateThread performs the actual append operation).<br />

Then, line 66 updates writeLocation for the next call <strong>to</strong> setSharedInt. The<br />

output continues with the current writeLocation and readLocation values and the<br />

values in the circular buffer (lines 69–73). If the writeLocation is equal <strong>to</strong> the read-<br />

Location, the circular buffer is currently full, so writeable is set <strong>to</strong> false (line 77)<br />

and the program displays the string BUFFER FULL (lines 80–81). Finally, line 85 invokes<br />

method notify <strong>to</strong> indicate that a waiting thread should move <strong>to</strong> the ready state.<br />

Method getSharedInt (lines 92–149) also performs the same tasks in this example<br />

as it did in Fig. 15.10, with a few minor modifications. When execution continues at line<br />

115 after the while loop, writeable is set <strong>to</strong> true because there will be at least one<br />

open position in the buffer in which the producer can place a value. Next, line 118 assigns<br />

value the value at readLocation in the circular buffer. Lines 121–123 append <strong>to</strong> the<br />

JTextArea the value consumed and the cell from which the value was read. Then, line<br />

126 updates readLocation for the next call <strong>to</strong> method getSharedInt. Lines 129–<br />

131 continue the output in the JTextArea with the current writeLocation and<br />

readLocation values and the current values in the circular buffer. If the readLocation<br />

is equal <strong>to</strong> the writeLocation, the circular buffer is currently empty, so readable<br />

is set <strong>to</strong> false (line 137), and lines 140–141 display the string BUFFER EMPTY.<br />

Finally, line 145 invokes method notify <strong>to</strong> place the next waiting thread in<strong>to</strong> the ready<br />

state, and line 147 returns the retrieved value <strong>to</strong> the calling method.<br />

In this version of the program, the outputs include the current writeLocation and<br />

readLocation values and the current contents of the buffer sharedInt. The elements<br />

of the sharedInt array were initialized <strong>to</strong> –1 for output purposes so that you can see each<br />

value inserted in the buffer. Notice that after the program places the fifth value in the fifth<br />

element of the buffer, the program inserts the sixth value at the beginning of the array—<br />

thus providing the circular buffer effect. Method displayBuffer (lines 152–166) uses

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

Saved successfully!

Ooh no, something went wrong!