|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.faunos.util.cc.LockStack
public class LockStack
Utility for lock acquisition and unwinding. Mostly, this has to do with the fact that exception-safe code in Java ends up with a clutter of try/finally clauses. (Language envy: C++ shines here..)
LockStack locks = new LockStack(); try { Lock a = .. locks.lock(a); Lock b = .. if (!locks.tryLock(b)) return; Lock c = .. Lock d = .. if (!locks.tryLock(c)) locks.lock(d); // do some work .. } finally { locks.unwindAll(); }
Constructor Summary | |
---|---|
LockStack()
|
Method Summary | |
---|---|
void |
lock(Lock lock)
Acquires the given lock and places it on the
stack. |
int |
size()
Returns the number of locks held in this stack. |
boolean |
tryLock(Lock lock)
Attempts to acquire the given lock and returns
the result. |
void |
unlockFirst()
Removes the first lock from the bottom of the stack and unlocks it. |
void |
unlockLast()
Removes the last lock from the top of the stack and unlocks it. |
void |
unwind(int count)
Unwinds (unlocks) the last count locks in reverse of the
order in which they were acquired. |
void |
unwindAll()
Unwinds (unlocks) all the acquired locks in reverse of the order in which they were acquired. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public LockStack()
Method Detail |
---|
public boolean tryLock(Lock lock)
lock
and returns
the result. If successful, the lock
is placed
on top of the stack.
public void lock(Lock lock)
lock
and places it on the
stack.
public void unlockLast()
IllegalStateException
- if the stack is emptypublic void unlockFirst()
IllegalStateException
- if the stack is emptypublic int size()
public void unwindAll()
public void unwind(int count)
count
locks in reverse of the
order in which they were acquired.
On return, the stack is count
locks smaller.
IllegalArgumentException
- if count
> size()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |