Class GcsLock

    • Method Detail

      • tryLock

        public boolean tryLock()
        Acquires the lock only if it is free at the time of invocation.

        Acquires the lock if it is available and returns immediately with the value true. If the lock is not available then this method will return immediately with the value false.

        Any exception would be returned as the parameter in the callback GcsLockListener#acquireLockException(java.lang.Exception)

        Specified by:
        tryLock in interface Lock
        Returns:
        true if the lock was acquired and false otherwise
      • tryLock

        public boolean tryLock​(long time,
                               TimeUnit timeUnit)
                        throws InterruptedException
        Acquires the lock if it is free within the given waiting time and the current thread has not been interrupted.

        If the lock is available this method returns immediately with the value true. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

        • The lock is acquired by the current thread; or
        • Some other thread interrupts the current thread, and interruption of lock acquisition is supported; or
        • The specified waiting time elapses

        If the lock is acquired then the value true is returned.

        If the current thread:

        • has its interrupted status set on entry to this method; or
        • is interrupted while acquiring the lock, and interruption of lock acquisition is supported,
        then InterruptedException is thrown and the current thread's interrupted status is cleared.

        If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

        Specified by:
        tryLock in interface Lock
        Parameters:
        time - the maximum time to wait for the lock
        timeUnit - the time unit of the time argument
        Returns:
        true if the lock was acquired and false if the waiting time elapsed before the lock was acquired
        Throws:
        InterruptedException - if the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported)
      • lock

        public void lock()
        Acquires the lock.

        If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.

        Any exception would be returned as the parameter in the callback GcsLockListener#acquireLockException(java.lang.Exception)

        Specified by:
        lock in interface Lock
      • lockInterruptibly

        public void lockInterruptibly()
                               throws InterruptedException
        Acquires the lock unless the current thread is interrupted.

        Acquires the lock if it is available and returns immediately.

        If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:

        • The lock is acquired by the current thread; or
        • Some other thread interrupts the current thread, and interruption of lock acquisition is supported.

        If the current thread:

        • has its interrupted status set on entry to this method; or
        • is interrupted while acquiring the lock, and interruption of lock acquisition is supported,
        then InterruptedException is thrown and the current thread's interrupted status is cleared.
        Specified by:
        lockInterruptibly in interface Lock
        Throws:
        InterruptedException - if the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported)
      • isLocked

        public boolean isLocked()
        Returns:
        true if the lock has been acquired and false otherwise
      • isHeldByCurrentThread

        public boolean isHeldByCurrentThread()
        Returns:
        true if the lock was acquired by the current thread false otherwise
      • addLockListener

        public void addLockListener​(GcsLockListener listener)
        Add a lock listener as the callback for state change
        Parameters:
        listener -
      • removeLockListener

        public void removeLockListener​(GcsLockListener listener)
        Remove a lock listener as the callback for state change
        Parameters:
        listener -