Class InfinispanLock
java.lang.Object
org.infinispan.util.concurrent.locks.impl.InfinispanLock
A special lock for Infinispan cache.
The main different with the traditional
Lock
is allowing to use any object as lock
owner. It is possible to use a Thread
as lock owner that makes similar to Lock
.
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 a ExtendedLockPromise
. 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
ConstructorDescriptionInfinispanLock
(Executor nonBlockingExecutor, TimeService timeService) Creates a new instance.InfinispanLock
(Executor nonBlockingExecutor, TimeService timeService, Runnable releaseRunnable) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionIt 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.boolean
isLocked()
It checks if the lock is acquired.void
It tries to release the lock held bylockOwner
.void
setTimeService
(TimeService timeService) Tests purpose only!
-
Constructor Details
-
InfinispanLock
Creates a new instance.- Parameters:
nonBlockingExecutor
- executor that is resumed upon after a lock has been acquired or times out if waitingtimeService
- theTimeService
to check for timeouts.
-
InfinispanLock
public InfinispanLock(Executor nonBlockingExecutor, 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
- theTimeService
to check for timeouts.releaseRunnable
- aRunnable
that is invoked every time this lock is released.
-
-
Method Details
-
setTimeService
Tests purpose only! -
acquire
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
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
- 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
It forces a deadlock checking. -
containsLockOwner
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.
-