public class PutTakeTest {//from Java Concurrency by Brian Goetz private static final ExecutorService pool = Executors.newCachedThreadPool(); private final AtomicInteger putSum = new AtomicInteger(0); private final AtomicInteger takeSum = new AtomicInteger(0); private final CyclicBarrier barrier; private final BoundedBuffer bb; private final int nTrials, nPairs; public static void main(String[] args) { new PutTakeTest(10,10,100000).test(); //sample parameters pool.shutdown(); } PutTakeTest(int capacity, int npairs, int ntrials) { this.bb = new BoundedBuffer(capacity); this.nTrials = ntrials; this.nPairs = npairs; this.barrier = new CyclicBarrier(npairs*2+1); } void test() { try { for (int i=0; i 0; --i) { bb.put(seed); sum+= seed; seed = xorShift(seed); } putSum.getAndAdd(sum); barrier.await(); } catch (Exception e) { throw new RuntimeException(e); } } } Class Consumer implements Runnable { public void run() { try{ barrier.await(); int sum =0; for (int i = nTrials; i>0; --i) { sum+= bb.take(); } takeSum.getAndAdd(sum); barrier.await(); } catch (Exception e) { throw new RuntimeException(e); } } } }