Class InfinispanLock
- java.lang.Object
-
- org.infinispan.util.concurrent.locks.impl.InfinispanLock
-
public class InfinispanLock extends Object
A special lock for Infinispan cache. The main different with the traditionalLockis allowing to use any object as lock owner. It is possible to use aThreadas lock owner that makes similar toLock. In addition, it has an asynchronous interface.acquire(Object, long, TimeUnit)will not acquire the lock immediately (except if it is free) but will return aExtendedLockPromise. This promise allow to test if the lock is acquired asynchronously and cancel the lock acquisition, without any blocking.- Since:
- 8.0
- Author:
- Pedro Ruivo
-
-
Constructor Summary
Constructors Constructor Description InfinispanLock(Executor nonBlockingExecutor, org.infinispan.commons.time.TimeService timeService)Creates a new instance.InfinispanLock(Executor nonBlockingExecutor, org.infinispan.commons.time.TimeService timeService, Runnable releaseRunnable)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ExtendedLockPromiseacquire(Object lockOwner, long time, TimeUnit timeUnit)It tries to acquire this lock.booleancontainsLockOwner(Object lockOwner)It tests if the lock has the lock owner.voiddeadlockCheck(DeadlockChecker deadlockChecker)It forces a deadlock checking.ObjectgetLockOwner()booleanisLocked()It checks if the lock is acquired.voidrelease(Object lockOwner)It tries to release the lock held bylockOwner.voidsetTimeService(org.infinispan.commons.time.TimeService timeService)Tests purpose only!
-
-
-
Constructor Detail
-
InfinispanLock
public InfinispanLock(Executor nonBlockingExecutor, org.infinispan.commons.time.TimeService timeService)
Creates a new instance.- Parameters:
nonBlockingExecutor- executor that is resumed upon after a lock has been acquired or times out if waitingtimeService- theTimeServiceto check for timeouts.
-
InfinispanLock
public InfinispanLock(Executor nonBlockingExecutor, org.infinispan.commons.time.TimeService timeService, Runnable releaseRunnable)
Creates a new instance.- Parameters:
nonBlockingExecutor- executor that is resumed upon after a lock has been acquired or times out if waitingtimeService- theTimeServiceto check for timeouts.releaseRunnable- aRunnablethat is invoked every time this lock is released.
-
-
Method Detail
-
setTimeService
public void setTimeService(org.infinispan.commons.time.TimeService timeService)
Tests purpose only!
-
acquire
public ExtendedLockPromise acquire(Object lockOwner, long time, TimeUnit timeUnit)
It tries to acquire this lock. If it is invoked multiple times with the same owner, the sameExtendedLockPromiseis returned until it has timed-out orrelease(Object)is invoked. If the lock is free, it is immediately acquired, otherwise the lock owner is queued.- Parameters:
lockOwner- the lock owner who needs to acquire the lock.time- the timeout value.timeUnit- the timeout unit.- Returns:
- an
ExtendedLockPromise. - Throws:
NullPointerException- iflockOwnerortimeUnitisnull.
-
release
public void release(Object lockOwner)
It tries to release the lock held bylockOwner. If the lock is not acquired (is waiting or timed out/deadlocked) bylockOwner, itsExtendedLockPromiseis canceled. IflockOwneris the current lock owner, the lock is released and the next lock owner available will acquire the lock. If thelockOwnernever tried to acquire the lock, this method does nothing.- Parameters:
lockOwner- the lock owner who wants to release the lock.- Throws:
NullPointerException- iflockOwnerisnull.
-
getLockOwner
public Object getLockOwner()
- Returns:
- the current lock owner or
nullif it is not acquired.
-
isLocked
public boolean isLocked()
It checks if the lock is acquired. Afalsereturn value does not mean the lock is free since it may have queued lock owners.- Returns:
trueif the lock is acquired.
-
deadlockCheck
public void deadlockCheck(DeadlockChecker deadlockChecker)
It forces a deadlock checking.
-
containsLockOwner
public boolean containsLockOwner(Object lockOwner)
It tests if the lock has the lock owner. It returntrueif the lock owner is the current lock owner or it in the queue.- Parameters:
lockOwner- the lock owner to test.- Returns:
trueif it contains the lock owner.
-
-