Class UnsynchronizedBuffer Does Not Synchronize Access to the Buffer
Class UnsynchronizedBuffer
(Fig. 23.12) implements interface Buffer
(line 4), but does not synchronize access to the buffer’s state—we purposely do this to demonstrate the problems that occur when multiple threads access shared mutable data in without synchronization. Line 6 declares instance variable buffer
and initializes it to -1
. This value is used to demonstrate the case in which the Consumer
attempts to consume a value before the Producer
ever places a value in buffer
. Again, methods blockingPut
(lines 9–13) and blockingGet
(lines 16–20) do not synchronize access to the buffer
instance variable. Method blockingPut
simply assigns its argument to buffer
(line 12), and method ...
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.