Java Reference
In-Depth Information
Listing 12.13. Driver Programfor
TimedPutTakeTest
.
12.2.2. Comparing Multiple Algorithms
While
BoundedBuffer
is a fairly solid implementation that performs reasonably well,
it turns out to be no match for either
ArrayBlockingQueue
or
LinkedBlockin-
gQueue
(which explains why this buffer algorithm wasn't selected for inclusion in the class
library). The
java.util.concurrent
algorithms have been selected and tuned, in part
using tests just like those described here, to be as efficient as we know how to make them,
fares poorly is that
put
and
take
each have multiple operations that could encouter conten-
tion—acquire a semaphore, acquire a lock, release a semaphore. Other implementation ap-
proaches have fewer points at which they might contend with another thread.
Figure 12.2
shows comparative throughput on a dual hyperthreaded machine for all three
classes with 256-element buffers, using a variant of
TimedPutTakeTest
. This test sug-
gests that
LinkedBlockingQueue
scales better than
ArrayBlockingQueue
. This
may seem odd at first: a linked queue must allocate a link node object for each insertion,
and hence seems to be doing more work than the array-based queue. However, even though