Class FileLockNamedLock

  • All Implemented Interfaces:
    java.lang.AutoCloseable, NamedLock

    public final class FileLockNamedLock
    extends NamedLockSupport
    Named lock that uses FileLock. An instance of this class is about ONE LOCK (one file) and is possibly used by multiple threads. Each thread (if properly coded re boxing) will try to obtain either shared or exclusive lock. As file locks are JVM-scoped (so one JVM can obtain same file lock only once), the threads share file lock and synchronize according to it. Still, as file lock obtain operation does not block (or in other words, the method that does block cannot be controlled for how long it blocks), we are "simulating" thread blocking using Retry utility. This implementation performs coordination not only on thread (JVM-local) level, but also on process level, as long as other parties are using this same "advisory" locking mechanism.
    Since:
    1.7.3
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.locks.ReentrantLock criticalRegion
      Lock protecting "critical region": this is where threads are allowed to perform locking but should leave this region as quick as possible.
      private java.nio.channels.FileChannel fileChannel
      The FileChannel this instance is about.
      private java.util.concurrent.atomic.AtomicReference<java.nio.channels.FileLock> fileLockRef
      The reference of FileLock, if obtained.
      private static long LOCK_POSITION  
      private static long LOCK_SIZE  
      private static long RETRY_SLEEP_MILLIS  
      private java.util.Map<java.lang.Thread,​java.util.Deque<java.lang.Boolean>> threadSteps
      Thread -> steps stack (true = shared, false = exclusive)
    • Field Detail

      • threadSteps

        private final java.util.Map<java.lang.Thread,​java.util.Deque<java.lang.Boolean>> threadSteps
        Thread -> steps stack (true = shared, false = exclusive)
      • fileChannel

        private final java.nio.channels.FileChannel fileChannel
        The FileChannel this instance is about.
      • fileLockRef

        private final java.util.concurrent.atomic.AtomicReference<java.nio.channels.FileLock> fileLockRef
        The reference of FileLock, if obtained.
      • criticalRegion

        private final java.util.concurrent.locks.ReentrantLock criticalRegion
        Lock protecting "critical region": this is where threads are allowed to perform locking but should leave this region as quick as possible.
    • Constructor Detail

      • FileLockNamedLock

        public FileLockNamedLock​(java.lang.String name,
                                 java.nio.channels.FileChannel fileChannel,
                                 NamedLockFactorySupport factory)
    • Method Detail

      • doLockShared

        protected boolean doLockShared​(long time,
                                       java.util.concurrent.TimeUnit unit)
                                throws java.lang.InterruptedException
        Specified by:
        doLockShared in class NamedLockSupport
        Throws:
        java.lang.InterruptedException
      • doLockExclusively

        protected boolean doLockExclusively​(long time,
                                            java.util.concurrent.TimeUnit unit)
                                     throws java.lang.InterruptedException
        Specified by:
        doLockExclusively in class NamedLockSupport
        Throws:
        java.lang.InterruptedException
      • doLockShared

        private java.lang.Boolean doLockShared()
      • doLockExclusively

        private java.lang.Boolean doLockExclusively()
      • anyOtherThreadHasSteps

        private boolean anyOtherThreadHasSteps()
        Returns true if any other than this thread using this instance has any step recorded.
      • obtainFileLock

        private java.nio.channels.FileLock obtainFileLock​(boolean shared)
        Attempts to obtain real FileLock, returns non-null value is succeeds, or null if cannot.