Class InterProcessLock


  • public final class InterProcessLock
    extends Object
    Provides an inter-process locking signal that can be used to negotiate access across JVMs. Whoever holds the lock has it until it's released by the owner. A lock should therefore be released as soon as the lock is no longer needed. In most cases, the following pattern should be used:
     
     InterProcessLock lock = ...;
     lock.lock();
     try {
       // do work
     } finally {
       lock.unlock();
     }

    Locks remaining when the JVM exits will be released automatically.

    • Constructor Detail

      • InterProcessLock

        public InterProcessLock​(String id)
        Creates a new lock with the specified id.
        Parameters:
        id - an identifier
      • InterProcessLock

        public InterProcessLock​(File file)
        Creates a new lock using the specified file.
        Parameters:
        file - the file to use as a lock
    • Method Detail

      • lock

        public boolean lock()
                     throws LockException
        Acquire the lock.
        Returns:
        returns true if a lock was acquired, false otherwise
        Throws:
        LockException - if an error occurs when acquiring the lock
      • unlock

        public void unlock()
        Releases a previously acquired lock.