ResourceLockManager { public void sharedLock() { try { sharedResourceLock.lock(); if(sharedLocks == 0) { if(ownerThreadId == -1 || Thread.currentThread().getId() != ownerThreadId) { exclusiveResourceLock.acquire(); } } try { if(resourceValid) { sharedLocks++; } else { throw new LockingException("Resource is no longer valid, lock not allowed"); } } finally { sharedResourceLock.unlock(); } } catch(InterruptedException e) { throw new RuntimeException("Unable to get readlock", e); } } ResourceLockManager(); void exclusiveLock(); int getSharedLocks(); void exclusiveUnlock(); void exclusiveUnlock(boolean invalidate); void sharedLock(); void sharedUnlock(); boolean isResourceValid();  }
@Test public void testSharedLock() throws TimeoutException, InterruptedException, ExecutionException { final ResourceLockManager lockManager = new ResourceLockManager(); try { lockManager.sharedLock(); lockManager.sharedLock(); lockManager.sharedLock(); } finally { lockManager.sharedUnlock(); lockManager.sharedUnlock(); lockManager.sharedUnlock(); } }