FileDocCategorySizeDatePackage
Lock.javaAPI DocApache Lucene 1.4.33778Tue May 11 19:43:28 BST 2004org.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,v 1.12 2004/05/11 17:43:28 cutting Exp $
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.