Class CycleDetectingLockFactory.WithExplicitOrdering<E extends Enum<E>>
- Type Parameters:
E- The Enum type representing the explicit lock ordering.
- Enclosing class:
CycleDetectingLockFactory
CycleDetectingLockFactory.WithExplicitOrdering provides the additional enforcement of
an application-specified ordering of lock acquisitions. The application defines the allowed
ordering with an Enum whose values each correspond to a lock type. The order in which
the values are declared dictates the allowed order of lock acquisition. In other words, locks
corresponding to smaller values of Enum.ordinal() should only be acquired before locks
with larger ordinals. Example:
enum MyLockOrder {
FIRST, SECOND, THIRD;
}
CycleDetectingLockFactory.WithExplicitOrdering<MyLockOrder> factory =
CycleDetectingLockFactory.newInstanceWithExplicitOrdering(Policies.THROW);
Lock lock1 = factory.newReentrantLock(MyLockOrder.FIRST);
Lock lock2 = factory.newReentrantLock(MyLockOrder.SECOND);
Lock lock3 = factory.newReentrantLock(MyLockOrder.THIRD);
lock1.lock();
lock3.lock();
lock2.lock(); // will throw an IllegalStateException
As with all locks created by instances of CycleDetectingLockFactory explicitly
ordered locks participate in general cycle detection with all other cycle detecting locks, and
a lock's behavior when detecting a cyclic lock acquisition is defined by the Policy of
the factory that created it.
Note, however, that although multiple locks can be created for a given Enum value, whether it be through separate factory instances or through multiple calls to the same factory, attempting to acquire multiple locks with the same Enum value (within the same thread) will result in an IllegalStateException regardless of the factory's policy. For example:
CycleDetectingLockFactory.WithExplicitOrdering<MyLockOrder> factory1 =
CycleDetectingLockFactory.newInstanceWithExplicitOrdering(...);
CycleDetectingLockFactory.WithExplicitOrdering<MyLockOrder> factory2 =
CycleDetectingLockFactory.newInstanceWithExplicitOrdering(...);
Lock lockA = factory1.newReentrantLock(MyLockOrder.FIRST);
Lock lockB = factory1.newReentrantLock(MyLockOrder.FIRST);
Lock lockC = factory2.newReentrantLock(MyLockOrder.FIRST);
lockA.lock();
lockB.lock(); // will throw an IllegalStateException
lockC.lock(); // will throw an IllegalStateException
lockA.lock(); // reentrant acquisition is okay
It is the responsibility of the application to ensure that multiple lock instances with the same rank are never acquired in the same thread.
- Since:
- 13.0
- Author:
- Darick Tong
-
Nested Class Summary
Nested classes/interfaces inherited from class CycleDetectingLockFactory
CycleDetectingLockFactory.Policies, CycleDetectingLockFactory.Policy, CycleDetectingLockFactory.PotentialDeadlockException, CycleDetectingLockFactory.WithExplicitOrdering<E>Modifier and TypeClassDescriptionstatic enumPre-definedCycleDetectingLockFactory.Policyimplementations.static interfaceEncapsulates the action to be taken when a potential deadlock is encountered.static final classRepresents a detected cycle in lock acquisition ordering.static final classCycleDetectingLockFactory.WithExplicitOrdering<E extends Enum<E>>ACycleDetectingLockFactory.WithExplicitOrderingprovides the additional enforcement of an application-specified ordering of lock acquisitions. -
Method Summary
Modifier and TypeMethodDescriptionnewReentrantLock(E rank) Equivalent tonewReentrantLock(rank, false).newReentrantLock(E rank, boolean fair) Creates aReentrantLockwith the given fairness policy and rank.newReentrantReadWriteLock(E rank) Equivalent tonewReentrantReadWriteLock(rank, false).newReentrantReadWriteLock(E rank, boolean fair) Creates aReentrantReadWriteLockwith the given fairness policy and rank.Methods inherited from class CycleDetectingLockFactory
newInstance, newInstanceWithExplicitOrdering, newReentrantLock, newReentrantLock, newReentrantReadWriteLock, newReentrantReadWriteLockModifier and TypeMethodDescriptionstatic CycleDetectingLockFactoryCreates a new factory with the specified policy.static <E extends Enum<E>>
CycleDetectingLockFactory.WithExplicitOrdering<E> newInstanceWithExplicitOrdering(Class<E> enumClass, CycleDetectingLockFactory.Policy policy) Creates aCycleDetectingLockFactory.WithExplicitOrdering<E>.newReentrantLock(String lockName) Equivalent tonewReentrantLock(lockName, false).newReentrantLock(String lockName, boolean fair) Creates aReentrantLockwith the given fairness policy.newReentrantReadWriteLock(String lockName) Equivalent tonewReentrantReadWriteLock(lockName, false).newReentrantReadWriteLock(String lockName, boolean fair) Creates aReentrantReadWriteLockwith the given fairness policy.
-
Method Details
-
newReentrantLock
Equivalent tonewReentrantLock(rank, false). -
newReentrantLock
Creates aReentrantLockwith the given fairness policy and rank. The values returned byEnum.getDeclaringClass()andEnum.name()are used to describe the lock in warning or exception output.- Throws:
IllegalStateException- If the factory has already created aLockwith the specified rank.
-
newReentrantReadWriteLock
Equivalent tonewReentrantReadWriteLock(rank, false). -
newReentrantReadWriteLock
Creates aReentrantReadWriteLockwith the given fairness policy and rank. The values returned byEnum.getDeclaringClass()andEnum.name()are used to describe the lock in warning or exception output.- Throws:
IllegalStateException- If the factory has already created aLockwith the specified rank.
-