Package org.springframework.data.util
Interface Lock
public interface Lock
Lock
provides more extensive locking operations than can be obtained using synchronized
methods and
Lock
. It allows more flexible structuring and an improved usage model.
This Lock abstraction is an extension to the lock utilities
and intended for
easier functional and try-with-resources usage.
ReentrantLock backend = new ReentrantLock(); Lock lock = Lock.of(backend); lock.executeWithoutResult(() -> { // callback without returning a result }); lock.execute(() -> { // callback returning a result return …; });
- Since:
- 3.2
- Author:
- Mark Paluch
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
An acquired lock can be used with try-with-resources for easier releasing. -
Method Summary
Modifier and TypeMethodDescriptiondefault <T> T
Execute the action specified by the given callback object guarded by a lock and return its result.default void
executeWithoutResult
(Runnable action) Execute the action specified by the given callback object guarded by a lock.lock()
Acquires the lock.Acquires the lock unless the current thread is interrupted.static Lock
-
Method Details
-
of
- Parameters:
delegate
- must not be null.- Returns:
- a new
Lock
adapter.
-
lock
Lock.AcquiredLock 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.
- See Also:
-
lockInterruptibly
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,
InterruptedException
is thrown and the current thread's interrupted status is cleared.- Throws:
InterruptedException
-
execute
Execute the action specified by the given callback object guarded by a lock and return its result. Theaction
is only executed once the lock has been acquired.- Type Parameters:
T
- type of the result.- Parameters:
action
- the action to run.- Returns:
- the result of the action.
- Throws:
RuntimeException
- if thrown by the action
-
executeWithoutResult
Execute the action specified by the given callback object guarded by a lock. Theaction
is only executed once the lock has been acquired.- Parameters:
action
- the action to run.- Throws:
RuntimeException
- if thrown by the action.
-