Class Consumer
Class Consumer
(Fig. 23.11) also implements interface Runnable
, allowing the Consumer
to execute concurrently with the Producer
. Lines 11–14 initialize Buffer
reference sharedLocation
with an object that implements the Buffer
interface (created in main
, Fig. 23.13) and passed to the constructor as the parameter shared
. As we’ll see, this is the same UnsynchronizedBuffer
object that’s used to initialize the Producer
object—thus, the two threads share the same object. The Consumer
thread in this program performs the tasks specified in method run
(lines 17–39). Lines 21–34 iterate 10 times. Each iteration invokes Thread
method sleep
(line 26) to put the Consumer
thread into the timed waiting state for up to 3 seconds. Next, line ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.