@Test public void testTryLockAllWithTimeout_TimeoutSpansAllAcquisitions() throws Exception { final ReentrantLock unavailableLock = new ReentrantLock(); ExecutorService executor = Executors.newSingleThreadExecutor(); assertTrue(executor.submit(new ReentrantReadWriteUpdateLockTest.TryLockTask(unavailableLock)).get()); try { final AtomicBoolean lock1WasInitiallyAcquired = new AtomicBoolean(); final AtomicLong timeoutSuppliedToLock1 = new AtomicLong(Long.MIN_VALUE); HoldCountLock lock1 = new HoldCountLock(new ReentrantLock()){ @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { timeoutSuppliedToLock1.set(unit.toNanos(time)); boolean acquired = super.tryLock(time, unit); lock1WasInitiallyAcquired.set(acquired); Thread.sleep(1000); return acquired; } }; final AtomicBoolean lock2WasInitiallyAcquired = new AtomicBoolean(); final AtomicLong timeoutSuppliedToLock2 = new AtomicLong(Long.MAX_VALUE); HoldCountLock lock2 = new HoldCountLock(new ReentrantLock()){ @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { timeoutSuppliedToLock2.set(unit.toNanos(time)); boolean acquired = super.tryLock(time, unit); lock2WasInitiallyAcquired.set(acquired); return acquired; } }; final AtomicBoolean lock3WasInitiallyAcquired = new AtomicBoolean(); final AtomicLong timeoutSuppliedToLock3 = new AtomicLong(Long.MAX_VALUE); HoldCountLock lock3 = new HoldCountLock(unavailableLock) { @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { timeoutSuppliedToLock3.set(unit.toNanos(time)); boolean acquired = super.tryLock(time, unit); lock3WasInitiallyAcquired.set(acquired); return acquired; } }; boolean acquired = Locks.tryLockAll(1L, TimeUnit.NANOSECONDS, lock1, lock2, lock3); assertFalse(acquired); assertTrue(timeoutSuppliedToLock1.get() > 0); assertTrue(lock1WasInitiallyAcquired.get()); assertTrue(timeoutSuppliedToLock2.get() < 0); assertTrue(lock2WasInitiallyAcquired.get()); assertTrue(timeoutSuppliedToLock3.get() < 0); assertFalse(lock3WasInitiallyAcquired.get()); assertEquals(0, lock1.holdCount().value); assertEquals(0, lock2.holdCount().value); assertEquals(0, lock3.holdCount().value); } finally { executor.shutdown(); } }
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);  }