org.apache.lucene.store
abstract public class: Lock [javadoc |
source]
java.lang.Object
org.apache.lucene.store.Lock
Direct Known Subclasses:
SingleInstanceLock, NoLock, CheckedLock, DbLock, SimpleFSLock, NativeFSLock, JELock
An interprocess mutex lock.
Typical use might look like:
new Lock.With(directory.makeLock("my.lock")) {
public Object doBody() {
... code to execute while locked ...
}
}.run();
Also see:
- Directory#makeLock(String)
Nested Class Summary: |
---|
abstract public static class | Lock.With | Utility class for executing code with exclusive access. |
Field Summary |
---|
public static long | LOCK_POLL_INTERVAL | How long #obtain(long) waits, in milliseconds,
in between attempts to acquire the lock. |
public static final long | LOCK_OBTAIN_WAIT_FOREVER | Pass this value to #obtain(long) to try
forever to obtain the lock. |
protected Throwable | failureReason | If a lock obtain called, this failureReason may be set
with the "root cause" Exception as to why the lock was
not obtained. |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from org.apache.lucene.store.Lock Detail: |
abstract public boolean isLocked() throws IOException
Returns true if the resource is currently locked. Note that one must
still call #obtain() before using the resource. |
abstract public boolean obtain() throws IOException
Attempts to obtain exclusive access and immediately return
upon success or failure. |
public boolean obtain(long lockWaitTimeout) throws LockObtainFailedException, IOException {
failureReason = null;
boolean locked = obtain();
if (lockWaitTimeout < 0 && lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER)
throw new IllegalArgumentException("lockWaitTimeout should be LOCK_OBTAIN_WAIT_FOREVER or a non-negative number (got " + lockWaitTimeout + ")");
long maxSleepCount = lockWaitTimeout / LOCK_POLL_INTERVAL;
long sleepCount = 0;
while (!locked) {
if (lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER && sleepCount++ >= maxSleepCount) {
String reason = "Lock obtain timed out: " + this.toString();
if (failureReason != null) {
reason += ": " + failureReason;
}
LockObtainFailedException e = new LockObtainFailedException(reason);
if (failureReason != null) {
e.initCause(failureReason);
}
throw e;
}
try {
Thread.sleep(LOCK_POLL_INTERVAL);
} catch (InterruptedException ie) {
throw new ThreadInterruptedException(ie);
}
locked = obtain();
}
return locked;
}
Attempts to obtain an exclusive lock within amount of
time given. Polls once per #LOCK_POLL_INTERVAL
(currently 1000) milliseconds until lockWaitTimeout is
passed. |
abstract public void release() throws IOException
Releases exclusive access. |