FileDocCategorySizeDatePackage
Lock.javaAPI DocApache Lucene 2.2.04354Sat Jun 16 22:20:36 BST 2007org.apache.lucene.store

Lock

public abstract class Lock extends Object
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();
author
Doug Cutting
version
$Id: Lock.java 510176 2007-02-21 20:01:36Z mikemccand $
see
Directory#makeLock(String)

Fields Summary
public static long
LOCK_POLL_INTERVAL
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.
Constructors Summary
Methods Summary
public abstract booleanisLocked()
Returns true if the resource is currently locked. Note that one must still call {@link #obtain()} before using the resource.

public abstract booleanobtain()
Attempts to obtain exclusive access and immediately return upon success or failure.

return
true iff exclusive access is obtained

public booleanobtain(long lockWaitTimeout)
Attempts to obtain an exclusive lock within amount of time given. Currently polls once per second until lockWaitTimeout is passed.

param
lockWaitTimeout length of time to wait in ms
return
true if lock was obtained
throws
LockObtainFailedException if lock wait times out
throws
IOException if obtain() throws IOException


                                                       
         
    failureReason = null;
    boolean locked = obtain();
    int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL);
    int sleepCount = 0;
    while (!locked) {
      if (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 e) {
        throw new IOException(e.toString());
      }
      locked = obtain();
    }
    return locked;
  
public abstract voidrelease()
Releases exclusive access.