ucar.nc2.util
Class Mutex
java.lang.Object
ucar.nc2.util.Mutex
public class Mutex
- extends java.lang.Object
A simple non-reentrant mutual exclusion lock.
The lock is free upon construction. Each acquire gets the
lock, and each release frees it. Releasing a lock that
is already free has no effect.
This implementation makes no attempt to provide any fairness
or ordering guarantees. If you need them, consider using one of
the Semaphore implementations as a locking mechanism.
Sample usage
Mutex can be useful in constructions that cannot be
expressed using java synchronized blocks because the
acquire/release pairs do not occur in the same method or
code block. For example, you can use them for hand-over-hand
locking across the nodes of a linked list. This allows
extremely fine-grained locking, and so increases
potential concurrency, at the cost of additional complexity and
overhead that would normally make this worthwhile only in cases of
extreme contention.
JOHN CARON ADDITIONS:
Track which Thread has acquired the mutex, throw RunTimeException if wrong Thread
tries to release. You must match each release with exactly one acquire
[ Introduction to this package. ]
|
Constructor Summary |
Mutex()
|
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Mutex
public Mutex()
isLocked
public boolean isLocked()
getWho
public int getWho()
isLockedByMe
public boolean isLockedByMe()
acquire
public void acquire()
throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
release
public void release()
attempt
public boolean attempt(long msecs)
throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException