Class Lock

java.lang.Object
org.apache.lucene.store.Lock
All Implemented Interfaces:
Closeable, AutoCloseable

public abstract class Lock extends Object implements Closeable
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();
 
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Utility class for executing code with exclusive access.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Throwable
    If a lock obtain called, this failureReason may be set with the "root cause" Exception as to why the lock was not obtained.
    static final long
    Pass this value to obtain(long) to try forever to obtain the lock.
    static long
    How long obtain(long) waits, in milliseconds, in between attempts to acquire the lock.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    Releases exclusive access.
    abstract boolean
    Returns true if the resource is currently locked.
    abstract boolean
    Attempts to obtain exclusive access and immediately return upon success or failure.
    boolean
    obtain(long lockWaitTimeout)
    Attempts to obtain an exclusive lock within amount of time given.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LOCK_POLL_INTERVAL

      public static long LOCK_POLL_INTERVAL
      How long obtain(long) waits, in milliseconds, in between attempts to acquire the lock.
    • LOCK_OBTAIN_WAIT_FOREVER

      public static final long LOCK_OBTAIN_WAIT_FOREVER
      Pass this value to obtain(long) to try forever to obtain the lock.
      See Also:
    • failureReason

      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.
  • Constructor Details

    • Lock

      public Lock()
  • Method Details

    • obtain

      public abstract boolean obtain() throws IOException
      Attempts to obtain exclusive access and immediately return upon success or failure. Use close() to release the lock.
      Returns:
      true iff exclusive access is obtained
      Throws:
      IOException
    • obtain

      public boolean obtain(long lockWaitTimeout) throws IOException
      Attempts to obtain an exclusive lock within amount of time given. Polls once per LOCK_POLL_INTERVAL (currently 1000) milliseconds until lockWaitTimeout is passed.
      Parameters:
      lockWaitTimeout - length of time to wait in milliseconds or LOCK_OBTAIN_WAIT_FOREVER to retry forever
      Returns:
      true if lock was obtained
      Throws:
      LockObtainFailedException - if lock wait times out
      IllegalArgumentException - if lockWaitTimeout is out of bounds
      IOException - if obtain() throws IOException
    • close

      public abstract void close() throws IOException
      Releases exclusive access.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • isLocked

      public abstract boolean isLocked() throws IOException
      Returns true if the resource is currently locked. Note that one must still call obtain() before using the resource.
      Throws:
      IOException