@Test public void testLockAll_Rollback() throws Exception { HoldCountLock lock1 = new HoldCountLock(new ReentrantLock()); HoldCountLock lock2 = new HoldCountLock(new ReentrantLock()) { @Override public void lock() { throw new RuntimeException(); } }; Exception expected = null; try { Locks.lockAll(lock1, lock2); } catch (RuntimeException e) { expected = e; } assertNotNull(expected); assertEquals(0, lock1.holdCount().value); assertEquals(0, lock2.holdCount().value); }
public static <L extends Lock> void lockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); try { for (L lock : locks) { lock.lock(); stack.push(lock); } } catch (RuntimeException e) { unlockAll(stack); throw e; } }
Locks { public static <L extends Lock> void lockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); try { for (L lock : locks) { lock.lock(); stack.push(lock); } } catch (RuntimeException e) { unlockAll(stack); throw e; } } }
Locks { public static <L extends Lock> void lockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); try { for (L lock : locks) { lock.lock(); stack.push(lock); } } catch (RuntimeException e) { unlockAll(stack); throw e; } } Locks(); }
Locks { public static <L extends Lock> void lockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); try { for (L lock : locks) { lock.lock(); stack.push(lock); } } catch (RuntimeException e) { unlockAll(stack); throw e; } } 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> void lockAll(Iterable<L> locks) { Deque<L> stack = new LinkedList<L>(); try { for (L lock : locks) { lock.lock(); stack.push(lock); } } catch (RuntimeException e) { unlockAll(stack); throw e; } } 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);  }