Java Reference
In-Depth Information
Producer writes 5 5 true
Consumer reads 5 5 false
Consumer tries to read.
Buffer empty. Consumer waits. 5 false
Producer writes 6 6 true
Consumer reads 6 6 false
Producer writes 7 7 true
Consumer reads 7 7 false
Producer writes 8 8 true
Consumer reads 8 8 false
Producer writes 9 9 true
Consumer reads 9 9 false
Producer writes 10 10 true
Producer done producing
Terminating Producer
Consumer reads 10 10 false
Consumer read values totaling 55
Terminating Consumer
Fig. 23.21 | Two threads manipulating a synchronized buffer. (Part 3 of 3.)
23.10 Concurrent Collections
In Chapter 16, we introduced various collections from the Java Collections API. We also
mentioned that you can obtain synchronized versions of those collections to allow only one
thread at a time to access a collection that might be shared among several threads. The col-
lections from the java.util.concurrent package are specifically designed and optimized
for sharing collections among multiple threads.
Figure 23.22 lists the many concurrent collections in package java.util.concur-
rent . The entries for ConcurrentHashMap and LinkedBlockingQueue are shown in bold
because these are by far the most frequently used concurrent collections. Like the collec-
tions introduced in Chapter 16, the concurrent collections have been enhanced to support
lambdas. However, rather than providing methods to support streams, the concurrent col-
lections provide their own implementations of various stream-like operations—e.g., Con-
currentHashMap has methods forEach , reduce and search —that are designed and
optimized for concurrent collections that are shared among threads. For more information
on the concurrent collections, visit
 
 
Search WWH ::




Custom Search