Class MutuallyExclusiveSetLock<T>
- java.lang.Object
-
- com.palantir.util.MutuallyExclusiveSetLock<T>
-
public class MutuallyExclusiveSetLock<T> extends Object
This class accepts a collection ofComparable
objects, creates locks for each object, and then locks each object in order. This can be used to provide exclusive access at a per-object level instead of with broad locking.MutuallyExclusiveSetLock
locks the objects in the set in their natural order, or in the order determined by theComparator
you pass as a parameter.If you have an
equals()
method that is not consistent with yourcompareTo()
method, then you will most likely get a deadlock.This class assumes that the thread that is doing the locking is doing the work. If this thread blocks on another thread that tries to use this class, then deadlock may ensue.
Use a try-finally to unlock the classes and do not spawn threads or tasks that you block on that might run on other threads.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MutuallyExclusiveSetLock.LockState<K>
An instance of this class is returned by theMutuallyExclusiveSetLock.lockOnObjects()
method.
-
Constructor Summary
Constructors Constructor Description MutuallyExclusiveSetLock()
Deprecated.use factory methodcreate(boolean)
MutuallyExclusiveSetLock(boolean fair)
Deprecated.use factory methodcreate(boolean)
MutuallyExclusiveSetLock(boolean fair, Comparator<? super T> comparator)
Deprecated.use factory methodcreateWithComparator(boolean, Comparator)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T extends Comparable<? super T>>
MutuallyExclusiveSetLock<T>create(boolean fair)
static <T> MutuallyExclusiveSetLock<T>
createWithComparator(boolean fair, Comparator<? super T> comparator)
Constructs a newMutuallyExclusiveSetLock
that will lock the objects in the order determined bycomparator
.boolean
isLocked(Iterable<T> items)
Returnstrue
if all the items are locked on the current thread.MutuallyExclusiveSetLock.LockState<T>
lockOnObjects(Iterable<T> lockObjects)
Attempts to acquire the locks in increasing order and may block.MutuallyExclusiveSetLock.LockState<T>
lockOnObjectsInterruptibly(Iterable<T> lockObjects)
void
unlock(MutuallyExclusiveSetLock.LockState<T> lockState)
Deprecated.
-
-
-
Constructor Detail
-
MutuallyExclusiveSetLock
@Deprecated public MutuallyExclusiveSetLock()
Deprecated.use factory methodcreate(boolean)
Constructs a newMutuallyExclusiveSetLock
with the fairness bit set tofalse
. When locks are under contention, no access order is guaranteed.- See Also:
MutuallyExclusiveSetLock(boolean)
-
MutuallyExclusiveSetLock
@Deprecated public MutuallyExclusiveSetLock(boolean fair)
Deprecated.use factory methodcreate(boolean)
Constructs a newMutuallyExclusiveSetLock
.- Parameters:
fair
- whentrue
, the class favors granting access to the longest-waiting thread when there is any contention. Whenfalse
, no access order is guaranteed.
-
MutuallyExclusiveSetLock
@Deprecated public MutuallyExclusiveSetLock(boolean fair, Comparator<? super T> comparator)
Deprecated.use factory methodcreateWithComparator(boolean, Comparator)
Constructs a newMutuallyExclusiveSetLock
that will lock the objects in the order determined bycomparator
.- Parameters:
fair
- whentrue
, the class favors granting access to the longest-waiting thread when there is any contention. Whenfalse
, no access order is guaranteed.comparator
- ajava.util.Comparator
to use in determining lock order.
-
-
Method Detail
-
create
public static <T extends Comparable<? super T>> MutuallyExclusiveSetLock<T> create(boolean fair)
-
createWithComparator
public static <T> MutuallyExclusiveSetLock<T> createWithComparator(boolean fair, Comparator<? super T> comparator)
Constructs a newMutuallyExclusiveSetLock
that will lock the objects in the order determined bycomparator
.- Parameters:
fair
- whentrue
, the class favors granting access to the longest-waiting thread when there is any contention. Whenfalse
, no access order is guaranteed.comparator
- ajava.util.Comparator
to use in determining lock order.
-
isLocked
public boolean isLocked(Iterable<T> items)
Returnstrue
if all the items are locked on the current thread.- Parameters:
items
- collection of items, not null
-
lockOnObjects
public MutuallyExclusiveSetLock.LockState<T> lockOnObjects(Iterable<T> lockObjects)
Attempts to acquire the locks in increasing order and may block.Be sure that the
Comparator<T>
orT.compareTo()
is consistent withT.equals()
. 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.- Returns:
LockState
instance with the information required to unlock these same objects.- See Also:
unlock(LockState)
-
lockOnObjectsInterruptibly
public MutuallyExclusiveSetLock.LockState<T> lockOnObjectsInterruptibly(Iterable<T> lockObjects) throws InterruptedException
- Throws:
InterruptedException
-
unlock
@Deprecated public void unlock(MutuallyExclusiveSetLock.LockState<T> lockState)
Deprecated.Unlocks the objects acquired from locking. This method should always be in a finally block immediately after the lock. If you try to unlock from another thread, no objects are unlocked.- Parameters:
lockState
- object that was returned by thelockOnObjects()
method when you locked the objects- See Also:
lockOnObjects(Iterable)
-
-