Java Reference
In-Depth Information
while (ch != 'z');
Listing6-3 uses BlockingQueue 's put() and take() methods,respectively,to
put() blockswhenthereisnoroomtoputanobject; take() blockswhenthequeue
is empty.
Although BlockingQueue ensuresthatacharacterisneverconsumedbeforeitis
tion of the output from one run:
Y consumed by consumer.
Y produced by producer.
Z consumed by consumer.
Z produced by producer.
Chapter 4 's PC application overcame this incorrect output order by introducing an
extra layer of synchronization around setSharedChar() / Sys-
tem.out.println() and an extra layer of synchronization around
getSharedChar() / System.out.println() . The next section shows you an
alternative in the form of locks.
The java.util.concurrent.locks packageprovidesinterfacesandclassesfor
ation and monitors.
This package's most basic lock interface is Lock , which provides more extensive
lockingoperationsthancanbeachievedviathe synchronized reservedword. Lock
also supports a wait/notification mechanism through associated Condition objects.
Note The biggest advantage of Lock objects over the implicit locks that are ob-
tained when threads enter critical sections (controlled via the synchronized re-
Search WWH ::

Custom Search