|
ehcache | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.sf.ehcache.constructs.concurrent.Mutex
public class Mutex
class Node {
Object item;
Node next;
Mutex lock = new Mutex(); // each node keeps its own lock
Node(Object x, Node n) { item = x; next = n; }
}
class List {
protected Node head; // pointer to first node of list
// Use plain java synchronization to protect head field.
// (We could instead use a Mutex here too but there is no
// reason to do so.)
protected synchronized Node getHead() { return head; }
boolean search(Object x) throws InterruptedException {
Node p = getHead();
if (p == null) return false;
// (This could be made more compact, but for clarity of illustration,
// all of the cases that can arise are handled separately.)
p.lock.acquire(); // Prime loop by acquiring first lock.
// (If the acquire fails due to
// interrupt, the method will throw
// InterruptedException now,
// so there is no need for any
// further cleanup.)
for (;;) {
if (x.equals(p.item)) {
p.lock.release(); // release current before return
return true;
}
else {
Node nextp = p.next;
if (nextp == null) {
p.lock.release(); // release final lock that was held
return false;
}
else {
try {
nextp.lock.acquire(); // get next lock before releasing current
}
catch (InterruptedException ex) {
p.lock.release(); // also release current if acquire fails
throw ex;
}
p.lock.release(); // release old lock now that new one held
p = nextp;
}
}
}
}
synchronized void add(Object x) { // simple prepend
// The use of `synchronized' here protects only head field.
// The method does not need to wait out other traversers
// who have already made it past head.
head = new Node(x, head);
}
// ... other similar traversal and update methods ...
}
| Field Summary | |
|---|---|
protected boolean |
inUse
The lock status * |
| Fields inherited from interface net.sf.ehcache.constructs.concurrent.Sync |
|---|
ONE_CENTURY, ONE_DAY, ONE_HOUR, ONE_MINUTE, ONE_SECOND, ONE_WEEK, ONE_YEAR |
| Constructor Summary | |
|---|---|
Mutex()
|
|
| Method Summary | |
|---|---|
void |
acquire()
Wait (possibly forever) until successful passage. |
boolean |
attempt(long msecs)
Wait at most msecs to pass; report whether passed. |
void |
release()
Potentially enable others to pass. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected boolean inUse
| Constructor Detail |
|---|
public Mutex()
| Method Detail |
|---|
public void acquire()
throws java.lang.InterruptedException
acquire in interface Syncjava.lang.InterruptedExceptionSync.acquire()
public boolean attempt(long msecs)
throws java.lang.InterruptedException
Sync
attempt in interface Syncmsecs - the number of milleseconds to wait.
An argument less than or equal to zero means not to wait at all.
However, this may still require
access to a synchronization lock, which can impose unbounded
delay if there is a lot of contention among threads.
java.lang.InterruptedExceptionSync.attempt(long)public void release()
release in interface SyncSync.release()
|
ehcache | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||