LockMRPlusSW extends ReentrantLock implements Lock { @Override public void enterCriticalSection(final boolean readLockRequested) { if (!readLockRequested) lock(); }  @Override void enterCriticalSection(final boolean readLockRequested); @Override void leaveCriticalSection();  }
@Test public void testMultipleReadersAtATime() { final Lock testLock = new LockMRPlusSW(); testLock.enterCriticalSection(true); final AtomicBoolean secondReaderHasLock = new AtomicBoolean(); defaultThreadFactory().newThread(() -> { testLock.enterCriticalSection(true); secondReaderHasLock.set(true); }).start(); await().untilTrue(secondReaderHasLock); }
@Test public void testOneWriterAtATime() throws InterruptedException { final Lock testLock = new LockMRPlusSW(); testLock.enterCriticalSection(false); final AtomicBoolean secondWriterHasLock = new AtomicBoolean(); defaultThreadFactory().newThread(() -> { testLock.enterCriticalSection(false); secondWriterHasLock.set(true); }).start(); sleep(5000); assertFalse("Multiple writers were allowed!", secondWriterHasLock.get()); }
@Test public void testAWriterDoesNotBlockReaders() { final Lock testLock = new LockMRPlusSW(); testLock.enterCriticalSection(false); final AtomicBoolean readerHasLock = new AtomicBoolean(); defaultThreadFactory().newThread(() -> { testLock.enterCriticalSection(true); readerHasLock.set(true); }).start(); await().untilTrue(readerHasLock); }