... in this program performs the tasks specified in method run. Lines 19–29 in Fig. 23.11 iterate 10 times. Each iteration invokes Thread method sleep (line 22) to put the Consumer thread into the timed waiting state for up to 3 seconds. Next, line 23 uses the Buffer’s blockingGet method to retrieve the value in the shared buffer, then adds the value to variable sum. Line 24 displays the total of all the values consumed so far. When the loop completes, lines 31–32 display the sum of the consumed values. Then method run terminates, which indicates that the Consumer completed its task. Once both threads enter the terminated state, the program ends.

Fig. 23.11

1   // Fig. 23.11: Consumer.java
2   // Consumer with a run method that loops, reading 10 values ...

Get Java How to Program, Early Objects, 11th 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.