Class NoOpLock

  • All Implemented Interfaces:
    java.util.concurrent.locks.Lock

    public final class NoOpLock
    extends java.lang.Object
    implements java.util.concurrent.locks.Lock
    A Lock implementation that is a no operation, i.e. it effectively does nothing.

    Useful for effectively eliding a lock in a single threaded environment.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static NoOpLock INSTANCE
      As there is no instance state then this object can be used to save on allocation.
    • Constructor Summary

      Constructors 
      Constructor Description
      NoOpLock()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void lock()
      Proceeds as if the lock has been acquired.
      void lockInterruptibly()
      Proceeds as if the lock has been acquired.
      java.util.concurrent.locks.Condition newCondition()
      Not supported.
      boolean tryLock()
      Always succeeds.
      boolean tryLock​(long time, java.util.concurrent.TimeUnit unit)
      Always succeeds.
      void unlock()
      The lock has never been taken so no effect.
      • Methods inherited from class java.lang.Object

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

      • INSTANCE

        public static final NoOpLock INSTANCE
        As there is no instance state then this object can be used to save on allocation.
    • Constructor Detail

      • NoOpLock

        public NoOpLock()
    • Method Detail

      • lock

        public void lock()
        Proceeds as if the lock has been acquired.
        Specified by:
        lock in interface java.util.concurrent.locks.Lock
      • lockInterruptibly

        public void lockInterruptibly()
        Proceeds as if the lock has been acquired.
        Specified by:
        lockInterruptibly in interface java.util.concurrent.locks.Lock
      • tryLock

        public boolean tryLock()
        Always succeeds.
        Specified by:
        tryLock in interface java.util.concurrent.locks.Lock
        Returns:
        always true.
      • tryLock

        public boolean tryLock​(long time,
                               java.util.concurrent.TimeUnit unit)
        Always succeeds.
        Specified by:
        tryLock in interface java.util.concurrent.locks.Lock
        Parameters:
        time - which is ignored.
        unit - which is ignored.
        Returns:
        always true.
      • unlock

        public void unlock()
        The lock has never been taken so no effect.
        Specified by:
        unlock in interface java.util.concurrent.locks.Lock
      • newCondition

        public java.util.concurrent.locks.Condition newCondition()
        Not supported.
        Specified by:
        newCondition in interface java.util.concurrent.locks.Lock
        Returns:
        never returns.
        Throws:
        java.lang.UnsupportedOperationException - if used.