### Question:
LazySingleReference implements Supplier<T> { @Override public T get() { T result; if ( (result = reference) == null ) { synchronized ( this ) { if ( (result = reference) == null ) { result = reference = create(); } } } return result; }  boolean isCreated(); @Override T get(); synchronized void invalidate();  }### Answer:
@Test public void shouldOnlyAllowSingleThreadToInitialize() throws Exception { final CountDownLatch latch = new CountDownLatch( 1 ); final AtomicInteger initCalls = new AtomicInteger(); LazySingleReference<Integer> ref = new LazySingleReference<Integer>() { @Override protected Integer create() { awaitLatch( latch ); return initCalls.incrementAndGet(); } }; Future<Integer> t1Evaluate = t1.executeDontWait( evaluate( ref ) ); t1.waitUntilWaiting(); Future<Integer> t2Evaluate = t2.executeDontWait( evaluate( ref ) ); t2.waitUntilBlocked(); latch.countDown(); int e1 = t1Evaluate.get(); int e2 = t2Evaluate.get(); assertEquals( "T1 evaluation", 1, e1 ); assertEquals( "T2 evaluation", 1, e2 ); }