FileDocCategorySizeDatePackage
Lock.javaAPI DocApache Lucene 2.1.04079Wed Feb 14 10:46:40 GMT 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 472959 2006-11-09 16:21:50Z yonik $
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
IOException if lock wait times out or obtain() throws an 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;
        }
        IOException e = new IOException(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.