FileDocCategorySizeDatePackage
Lock.javaAPI DocApache Lucene 1.93774Mon Feb 20 09:20:16 GMT 2006org.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 179414 2005-06-01 20:10:58Z dnaber $
see
Directory#makeLock(String)

Fields Summary
public static long
LOCK_POLL_INTERVAL
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


                         
       

                                                      
        
    boolean locked = obtain();
    int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL);
    int sleepCount = 0;
    while (!locked) {
      if (sleepCount++ == maxSleepCount) {
        throw new IOException("Lock obtain timed out: " + this.toString());
      }
      try {
        Thread.sleep(LOCK_POLL_INTERVAL);
      } catch (InterruptedException e) {
        throw new IOException(e.toString());
      }
      locked = obtain();
    }
    return locked;
  
public abstract voidrelease()
Releases exclusive access.