CriticalSectionFactory { public Runnable wrap(Runnable runnable) { return () -> { if (isRunning.compareAndSet(false, true)) { try { runnable.run(); } finally { isRunning.set(false); } } }; }  Runnable wrap(Runnable runnable);  }
@Test(groups = "fast") public void testCriticalSectionFactory() throws InterruptedException { Runnable criticalSection = factory.wrap(runnable); Thread t1 = TestUtils.runInThread(criticalSection); entryLatch.await(); Thread t2 = TestUtils.runInThread(criticalSection); t2.join(); criticalSectionLatch.countDown(); t1.join(); Assert.assertEquals(count, 1, "too many critical section calls"); }