public class WaitLock
extends java.lang.Object
waitUntilUnlock(int)
to wait.
Meanwhile, once A completes the loading, it put back the resource
and calls unlock()
.
WaitLock lock = null;
for (;;) {
synchronized (map) {
Object o = map.get(key);
if (o == null) {
map.put(key, lock = new WaitLock());
break; //go to load resource
}
}
if (o instanceof MyResource)
return (MyResource)o;
if (!((Lock)o).waitUntilUnlock(60000))
log.waring("Takes too long");
}
//load resource
try {
....
synchronized (map) {
map.put(key, resource);
}
return resource;
} catch (Throwable ex) {
synchronized (map) {
map.remove(key);
}
throw SystemException.Aide.wrap(ex);
} finally {
lock.unlock();
}
Constructor and Description |
---|
WaitLock()
Once created, it is default to be locked.
|
Modifier and Type | Method and Description |
---|---|
void |
unlock()
Unlocks any other threads blocked by
waitUntilUnlock(int) . |
boolean |
waitUntilUnlock(int timeout)
Waits this lock to unlock.
|
public WaitLock()
waitUntilUnlock(int)
won't return until unlock()
is called.public boolean waitUntilUnlock(int timeout)
SystemException
- if this thread is interruptedPotentialDeadLockException
- if the thread itself creates
this lock. In other words, it tried to wait for itself to complete.public void unlock()
waitUntilUnlock(int)
.Copyright © 2005-2018 Potix Corporation. All Rights Reserved.