Class Producer
Class Producer (Fig. 23.10) implements the Runnable interface, allowing it to be executed as a task in a separate thread. The constructor (lines 11–14) initializes the Buffer reference sharedLocation with an object created in main (line 15 of Fig. 23.13) and passed to the constructor. As we’ll see, this is an UnsynchronizedBuffer object that implements interface Buffer without synchronizing access to the shared object. The Producer thread in this program executes the tasks specified in the method run (Fig. 23.10, lines 17–39). Each iteration of the loop (lines 21–35) invokes Thread method sleep (line 25) to place the Producer thread into the timed waiting state for a random time interval between 0 and 3 seconds. When the thread ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access