T - - the type on which the lock acts.public final class GroupLock<T> extends Object
Lock multiple values simultaneously. This intended for use for controlling
access by a number of threads to a number of resources, identified by values
of type T.
This is not re-entrant: if the same thread attempts to lock the same value(s) twice it will block until interrupted.
| Modifier and Type | Method and Description |
|---|---|
static <U> GroupLock<U> |
fromComparator(Comparator<? super U> comparator)
Creates a new lock which uses the ordering of the provided comparator to
determine the order in which locks on the value are acquired.
|
void |
lock(Set<T> ids)
Acquires locks on a number of ids progressively, in order specified by
this locks ordering.
|
void |
lock(T id)
Lock the provided
id so no other thread may lock it. |
static <C extends Comparable<? super C>> |
natural()
Creates a new lock which uses the natural ordering of the value type to
determine the order in which locks on the value are acquired.
|
boolean |
tryLock(Set<T> ids)
Attempt to lock all the ids.
|
boolean |
tryLock(T id)
Attempt to lock
id, if it is not already locked. |
void |
unlock(Set<T> ids)
Release locks on all the ids, if any were held.
|
void |
unlock(T id)
Release the lock held on
id. |
public static final <C extends Comparable<? super C>> GroupLock<C> natural()
public static final <U> GroupLock<U> fromComparator(Comparator<? super U> comparator)
public void lock(T id) throws InterruptedException
Lock the provided id so no other thread may lock it. If the
id is already locked then this thread blocks until awoken.
Whether or not this value has already been locked is determined by equality.
id - - the value on which to lock.InterruptedException - thread was interrupted whilst waiting for the lock.public void unlock(T id)
Release the lock held on id. Any threads also attempting to lock
this value will be awoken.
If the value is not locked then this call has no effect.
id - - the id to unlockInterruptedExceptionpublic boolean tryLock(T id) throws InterruptedException
id, if it is not already locked.id - - the value to attempt to lockInterruptedException - thread was interrupted whilst waiting for the lock.public void lock(Set<T> ids) throws InterruptedException
Acquires locks on a number of ids progressively, in order specified by this locks ordering.
E.g. if lock(Arrays.asList("A","B","C")) is called by Thread 1
whilst "C" is already locked by Thread 2 then Thread 1 blocks,
holding locks on "A" and "B", until it can lock
"C".
ids - - the values to lockInterruptedException - thread was interrupted whilst waiting for the lock.public void unlock(Set<T> ids)
ids - = the values to unlockpublic boolean tryLock(Set<T> ids) throws InterruptedException
ids - - the values to attempt to lockInterruptedException - thread was interrupted whilst waiting for the lock.Copyright © 2014. All rights reserved.