Class GcsLock
- java.lang.Object
-
- org.openbites.concurrent.locks.gcs.GcsLock
-
- All Implemented Interfaces:
Serializable,Lock,DistributedLock
public class GcsLock extends Object implements DistributedLock, Serializable
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description GcsLock(GcsLockConfig lockConfig)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddLockListener(GcsLockListener listener)Add a lock listener as the callback for state changebooleanisHeldByCurrentThread()booleanisLocked()voidlock()Acquires the lock.voidlockInterruptibly()Acquires the lock unless the current thread is interrupted.voidremoveLockListener(GcsLockListener listener)Remove a lock listener as the callback for state changebooleantryLock()Acquires the lock only if it is free at the time of invocation.booleantryLock(long time, TimeUnit timeUnit)Acquires the lock if it is free within the given waiting time and the current thread has not been interrupted.voidunlock()Releases the lock.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.openbites.concurrent.locks.DistributedLock
newCondition
-
-
-
-
Constructor Detail
-
GcsLock
public GcsLock(GcsLockConfig lockConfig)
-
-
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 valuefalse.Any exception would be returned as the parameter in the callback
GcsLockListener#acquireLockException(java.lang.Exception)
-
tryLock
public boolean tryLock(long time, TimeUnit timeUnit) throws InterruptedExceptionAcquires 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
trueis 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,
InterruptedExceptionis thrown and the current thread's interrupted status is cleared.If the specified waiting time elapses then the value
falseis returned. If the time is less than or equal to zero, the method will not wait at all.- Specified by:
tryLockin interfaceLock- Parameters:
time- the maximum time to wait for the locktimeUnit- the time unit of thetimeargument- Returns:
trueif the lock was acquired andfalseif 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)
-
lockInterruptibly
public void lockInterruptibly() throws InterruptedExceptionAcquires 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,
InterruptedExceptionis thrown and the current thread's interrupted status is cleared.- Specified by:
lockInterruptiblyin interfaceLock- Throws:
InterruptedException- if the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported)
-
unlock
public void unlock()
Releases the lock.While used in multi-thread environment only the thread that originally acquired the lock is able to release the lock.
Any exception would be returned as the parameter in the callback
GcsLockListener#releaseLockException(java.lang.Exception)
-
isLocked
public boolean isLocked()
- Returns:
trueif the lock has been acquired andfalseotherwise
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()
- Returns:
trueif the lock was acquired by the current threadfalseotherwise
-
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-
-
-