@Test public void testTryLockAll_RollbackOnFailure() throws Exception { HoldCountLock lock1 = new HoldCountLock(new ReentrantLock()); HoldCountLock lock2 = new HoldCountLock(new ReentrantLock()) { @Override public boolean tryLock() { return false; } }; boolean acquired = Locks.tryLockAll(lock1, lock2); assertFalse(acquired); assertEquals(0, lock1.holdCount().value); assertEquals(0, lock2.holdCount().value); }
public static <L extends Lock> boolean tryLockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); boolean success = false; try { for (L lock : locks) { success = lock.tryLock(); if (success) { stack.push(lock); } else { break; } } } catch (RuntimeException e) { unlockAll(stack); throw e; } if (!success) { unlockAll(stack); } return success; }
Locks { public static <L extends Lock> boolean tryLockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); boolean success = false; try { for (L lock : locks) { success = lock.tryLock(); if (success) { stack.push(lock); } else { break; } } } catch (RuntimeException e) { unlockAll(stack); throw e; } if (!success) { unlockAll(stack); } return success; } }
Locks { public static <L extends Lock> boolean tryLockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); boolean success = false; try { for (L lock : locks) { success = lock.tryLock(); if (success) { stack.push(lock); } else { break; } } } catch (RuntimeException e) { unlockAll(stack); throw e; } if (!success) { unlockAll(stack); } return success; } Locks(); }
Locks { public static <L extends Lock> boolean tryLockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); boolean success = false; try { for (L lock : locks) { success = lock.tryLock(); if (success) { stack.push(lock); } else { break; } } } catch (RuntimeException e) { unlockAll(stack); throw e; } if (!success) { unlockAll(stack); } return success; } Locks(); static void lockAll(Iterable<L> locks); static void lockInterruptiblyAll(Iterable<L> locks); static boolean tryLockAll(Iterable<L> locks); static boolean tryLockAll(long time, TimeUnit unit, Iterable<L> locks); static void unlockAll(Iterable<L> locks); @SuppressWarnings({"JavaDoc"}) static void lockAll(L... locks); @SuppressWarnings({"JavaDoc"}) static void lockInterruptiblyAll(L... locks); @SuppressWarnings({"JavaDoc"}) static boolean tryLockAll(L... locks); @SuppressWarnings({"JavaDoc"}) static boolean tryLockAll(long time, TimeUnit unit, L... locks); @SuppressWarnings({"JavaDoc"}) static void unlockAll(L... locks); }
Locks { public static <L extends Lock> boolean tryLockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); boolean success = false; try { for (L lock : locks) { success = lock.tryLock(); if (success) { stack.push(lock); } else { break; } } } catch (RuntimeException e) { unlockAll(stack); throw e; } if (!success) { unlockAll(stack); } return success; } Locks(); static void lockAll(Iterable<L> locks); static void lockInterruptiblyAll(Iterable<L> locks); static boolean tryLockAll(Iterable<L> locks); static boolean tryLockAll(long time, TimeUnit unit, Iterable<L> locks); static void unlockAll(Iterable<L> locks); @SuppressWarnings({"JavaDoc"}) static void lockAll(L... locks); @SuppressWarnings({"JavaDoc"}) static void lockInterruptiblyAll(L... locks); @SuppressWarnings({"JavaDoc"}) static boolean tryLockAll(L... locks); @SuppressWarnings({"JavaDoc"}) static boolean tryLockAll(long time, TimeUnit unit, L... locks); @SuppressWarnings({"JavaDoc"}) static void unlockAll(L... locks);  }