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 traditionalLock
is allowing to use any object as lock owner. It is possible to use aThread
as 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 executor, org.infinispan.commons.time.TimeService timeService)
Creates a new instance.InfinispanLock(Executor executor, 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 ExtendedLockPromise
acquire(Object lockOwner, long time, TimeUnit timeUnit)
It tries to acquire this lock.boolean
containsLockOwner(Object lockOwner)
It tests if the lock has the lock owner.void
deadlockCheck(DeadlockChecker deadlockChecker)
It forces a deadlock checking.Object
getLockOwner()
boolean
isLocked()
It checks if the lock is acquired.void
release(Object lockOwner)
It tries to release the lock held bylockOwner
.void
setTimeService(org.infinispan.commons.time.TimeService timeService)
Tests purpose only!
-
-
-
Constructor Detail
-
InfinispanLock
public InfinispanLock(Executor executor, org.infinispan.commons.time.TimeService timeService)
Creates a new instance.- Parameters:
executor
-timeService
- theTimeService
to check for timeouts.
-
InfinispanLock
public InfinispanLock(Executor executor, org.infinispan.commons.time.TimeService timeService, Runnable releaseRunnable)
Creates a new instance.- Parameters:
executor
-timeService
- theTimeService
to check for timeouts.releaseRunnable
- aRunnable
that 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 sameExtendedLockPromise
is 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
- iflockOwner
ortimeUnit
isnull
.
-
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
, itsExtendedLockPromise
is canceled. IflockOwner
is the current lock owner, the lock is released and the next lock owner available will acquire the lock. If thelockOwner
never tried to acquire the lock, this method does nothing.- Parameters:
lockOwner
- the lock owner who wants to release the lock.- Throws:
NullPointerException
- iflockOwner
isnull
.
-
getLockOwner
public Object getLockOwner()
- Returns:
- the current lock owner or
null
if it is not acquired.
-
isLocked
public boolean isLocked()
It checks if the lock is acquired. Afalse
return value does not mean the lock is free since it may have queued lock owners.- Returns:
true
if 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 returntrue
if the lock owner is the current lock owner or it in the queue.- Parameters:
lockOwner
- the lock owner to test.- Returns:
true
if it contains the lock owner.
-
-