Class 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:
    Directory.makeLock(String)
    • Field Detail

      • 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:
        Constant Field Values
      • 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 Detail

      • Lock

        public Lock()
    • Method Detail

      • 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
      • 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