Class MutuallyExclusiveSetLock<T>


  • public class MutuallyExclusiveSetLock<T>
    extends Object
    This class accepts a collection of Comparable 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 the Comparator you pass as a parameter.

    If you have an equals() method that is not consistent with your compareTo() 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.

    • Constructor Detail

      • MutuallyExclusiveSetLock

        @Deprecated
        public MutuallyExclusiveSetLock()
        Deprecated.
        use factory method create(boolean)
        Constructs a new MutuallyExclusiveSetLock with the fairness bit set to false. When locks are under contention, no access order is guaranteed.
        See Also:
        MutuallyExclusiveSetLock(boolean)
      • MutuallyExclusiveSetLock

        @Deprecated
        public MutuallyExclusiveSetLock​(boolean fair)
        Deprecated.
        use factory method create(boolean)
        Constructs a new MutuallyExclusiveSetLock.
        Parameters:
        fair - when true, the class favors granting access to the longest-waiting thread when there is any contention. When false, no access order is guaranteed.
      • MutuallyExclusiveSetLock

        @Deprecated
        public MutuallyExclusiveSetLock​(boolean fair,
                                        Comparator<? super T> comparator)
        Deprecated.
        Constructs a new MutuallyExclusiveSetLock that will lock the objects in the order determined by comparator.
        Parameters:
        fair - when true, the class favors granting access to the longest-waiting thread when there is any contention. When false, no access order is guaranteed.
        comparator - a java.util.Comparator to use in determining lock order.
    • Method Detail

      • createWithComparator

        public static <T> MutuallyExclusiveSetLock<T> createWithComparator​(boolean fair,
                                                                           Comparator<? super T> comparator)
        Constructs a new MutuallyExclusiveSetLock that will lock the objects in the order determined by comparator.
        Parameters:
        fair - when true, the class favors granting access to the longest-waiting thread when there is any contention. When false, no access order is guaranteed.
        comparator - a java.util.Comparator to use in determining lock order.
      • isLocked

        public boolean isLocked​(Iterable<T> items)
        Returns true 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> or T.compareTo() is consistent with T.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)