public LockState<T> lockOnObjectsInterruptibly(Iterable<T> lockObjects) throws InterruptedException { ImmutableSet<T> hashSet = validateLockInput(lockObjects); List<ReentrantLock> toUnlock = Lists.newArrayList(); try { final SortedMap<T, ReentrantLock> sortedLocks = getSortedLocks(hashSet); for (ReentrantLock lock : sortedLocks.values()) { lock.lockInterruptibly(); toUnlock.add(lock); } LockState<T> ret = new LockState<T>(sortedLocks.values(), this); threadSet.add(Thread.currentThread()); toUnlock.clear(); return ret; } finally { for (ReentrantLock reentrantLock : toUnlock) { reentrantLock.unlock(); } } }
/** * Attempts to acquire the locks in increasing order and may block. * * <p>Be sure that the <code>Comparator<T></code> or <code>T.compareTo()</code> * is consistent with <code>T.equals()</code>. You can only lock once on a thread * with a set of objects. If you wish to lock on more objects, * you must unlock then pass the new set of objects to be locked. * @return <code>LockState</code> instance with the information required to * unlock these same objects. * @see #unlock(LockState) */ public LockState<T> lockOnObjects(Iterable<T> lockObjects) { ImmutableSet<T> hashSet = validateLockInput(lockObjects); final SortedMap<T, ReentrantLock> sortedLocks = getSortedLocks(hashSet); for (ReentrantLock lock : sortedLocks.values()) { lock.lock(); } threadSet.add(Thread.currentThread()); return new LockState<T>(sortedLocks.values(), this); }
public LockState<T> lockOnObjectsInterruptibly(Iterable<T> lockObjects) throws InterruptedException { ImmutableSet<T> hashSet = validateLockInput(lockObjects); List<ReentrantLock> toUnlock = Lists.newArrayList(); try { final SortedMap<T, ReentrantLock> sortedLocks = getSortedLocks(hashSet); for (ReentrantLock lock : sortedLocks.values()) { lock.lockInterruptibly(); toUnlock.add(lock); } LockState<T> ret = new LockState<T>(sortedLocks.values(), this); threadSet.add(Thread.currentThread()); toUnlock.clear(); return ret; } finally { for (ReentrantLock reentrantLock : toUnlock) { reentrantLock.unlock(); } } }
/** * Attempts to acquire the locks in increasing order and may block. * * <p>Be sure that the <code>Comparator<T></code> or <code>T.compareTo()</code> * is consistent with <code>T.equals()</code>. You can only lock once on a thread * with a set of objects. If you wish to lock on more objects, * you must unlock then pass the new set of objects to be locked. * @return <code>LockState</code> instance with the information required to * unlock these same objects. * @see #unlock(LockState) */ public LockState<T> lockOnObjects(Iterable<T> lockObjects) { ImmutableSet<T> hashSet = validateLockInput(lockObjects); final SortedMap<T, ReentrantLock> sortedLocks = getSortedLocks(hashSet); for (ReentrantLock lock : sortedLocks.values()) { lock.lock(); } threadSet.add(Thread.currentThread()); return new LockState<T>(sortedLocks.values(), this); }