/** * Unlocks the objects acquired from locking. * This method should always be in a try/finally block immediately after the lock. * If you try to unlock from another thread, no objects are unlocked. * @see #lockOnObjects(Iterable) */ public void unlock() { setLock.unlock(this); } }
private Thread createThread(final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock, final Collection<String> toLock) { final Thread thread; thread = new Thread(() -> { LockState<String> locked = mutuallyExclusiveSetLock.lockOnObjects(toLock); try { //stuff } finally { mutuallyExclusiveSetLock.unlock(locked); } }); return thread; } }
@Test public void testSimpleNotBlock() throws Exception { final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("whatup", "dog")); final Thread thread; try { thread = createThread(mutuallyExclusiveSetLock, Arrays.asList("heyo")); thread.setDaemon(true); thread.start(); thread.join(10 * 1000); } finally { mutuallyExclusiveSetLock.unlock(lockOnObjects); } }
@Test public void testSimpleBlock() throws Exception { final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("whatup", "dog")); final Thread thread; try { thread = createThread(mutuallyExclusiveSetLock, Arrays.asList("whatup")); thread.setDaemon(true); thread.start(); Thread.sleep(100); Assert.assertTrue(thread.isAlive()); // assertEquals(2, mutuallyExclusiveSetLock.syncMap.size()); } finally { mutuallyExclusiveSetLock.unlock(lockOnObjects); } thread.join(10 * 1000); // assertFalse(mutuallyExclusiveSetLock.syncMap.get("dog").isLocked()); // assertEquals(0, mutuallyExclusiveSetLock.threadSet.size()); }
@Test public void testInterface() throws Exception { MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("whatev", "dog")); try { //stuff } finally { // assertEquals(2, mutuallyExclusiveSetLock.syncMap.size()); // assertEquals(1, mutuallyExclusiveSetLock.threadSet.size()); mutuallyExclusiveSetLock.unlock(lockOnObjects); // assertFalse(mutuallyExclusiveSetLock.syncMap.get("whatev").isHeldByCurrentThread()); // assertEquals(0, mutuallyExclusiveSetLock.threadSet.size()); } }
@Test public void testDoubleLock() throws Exception { final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("whatup", "dog")); try { mutuallyExclusiveSetLock.lockOnObjects(Arrays.asList("anything")); } catch(Exception e) { return; //expected } finally { mutuallyExclusiveSetLock.unlock(lockOnObjects); } Assert.fail(); //should have thrown }
@Test public void testThreadOwnsLocks() { final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); List<String> asList = Arrays.asList("whatup", "dog"); LockState<String> lockOnObjects = mutuallyExclusiveSetLock.lockOnObjects(asList); try { Assert.assertTrue(mutuallyExclusiveSetLock.isLocked(asList)); } finally { mutuallyExclusiveSetLock.unlock(lockOnObjects); } }
setLock.unlock(locked);
/** * Unlocks the objects acquired from locking. * This method should always be in a try/finally block immediately after the lock. * If you try to unlock from another thread, no objects are unlocked. * @see #lockOnObjects(Iterable) */ public void unlock() { setLock.unlock(this); } }