/** * Constructs a new <code>MutuallyExclusiveSetLock</code> that will * lock the objects in the order determined by <code>comparator</code>. * @param fair when <code>true</code>, the class favors granting access to the * longest-waiting thread when there is any contention. * When <code>false</code>, no access order is guaranteed. * @param comparator a <code>java.util.Comparator</code> to use in determining lock order. */ public static <T> MutuallyExclusiveSetLock<T> createWithComparator(boolean fair, Comparator<? super T> comparator) { return new MutuallyExclusiveSetLock<T>(fair, comparator); }
public static <T extends Comparable<? super T>> MutuallyExclusiveSetLock<T> create(boolean fair) { return new MutuallyExclusiveSetLock<T>(fair); }
@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 testThreadDoesNotOwnUnlocked() { final MutuallyExclusiveSetLock<String> mutuallyExclusiveSetLock = new MutuallyExclusiveSetLock<String>(); List<String> asList = Arrays.asList("whatup", "dog"); Assert.assertTrue(!mutuallyExclusiveSetLock.isLocked(asList)); }
@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); } }
/** * Constructs a new <code>MutuallyExclusiveSetLock</code> that will * lock the objects in the order determined by <code>comparator</code>. * @param fair when <code>true</code>, the class favors granting access to the * longest-waiting thread when there is any contention. * When <code>false</code>, no access order is guaranteed. * @param comparator a <code>java.util.Comparator</code> to use in determining lock order. */ public static <T> MutuallyExclusiveSetLock<T> createWithComparator(boolean fair, Comparator<? super T> comparator) { return new MutuallyExclusiveSetLock<T>(fair, comparator); }
@Test public void testThreadDoesNotOwnOtherLocked() { final MutuallyExclusiveSetLock<String> setLock = new MutuallyExclusiveSetLock<String>(); final List<String> toLock = Arrays.asList("whatup", "dog");
public static <T extends Comparable<? super T>> MutuallyExclusiveSetLock<T> create(boolean fair) { return new MutuallyExclusiveSetLock<T>(fair); }