/** * Create a BlockingReference best suited to multi-reader/single-writer * situations. In a SRSW case this instance may not perform quite as well. * * @param initialValue the initial value */ public static <V> BlockingReference<V> newMRSW(final V initialValue) { return new BlockingReference<V>(new PhasedLatch() { /* * Workaround for the fact that take() always calls await. Calling await() * on a phased latch by default waits on the <i>next</i> phase (after the * current one). We need to make sure we await on the previous phase * instead so we remember the previous phase. */ private final AtomicInteger currentPhase = new AtomicInteger(super.getPhase()); @Override public synchronized int getPhase() { try { return currentPhase.get(); } finally { currentPhase.set(super.getPhase()); } } }, initialValue); }
/** * Create a BlockingReference best suited to single-reader/single-writer * situations. In a MRSW case this instance may get missed signals if multiple * reader threads are all waiting on the value. * * @param initialValue the initial value */ public static <V> BlockingReference<V> newSRSW(final V initialValue) { return new BlockingReference<V>(new BooleanLatch(), initialValue); }