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

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    An acquired lock can be used with try-with-resources for easier releasing.
  • Method Summary

    Modifier and Type
    Method
    Description
    default <T> T
    execute(Supplier<T> action)
    Execute the action specified by the given callback object guarded by a lock and return its result.
    default void
    Execute the action specified by the given callback object guarded by a lock.
    Acquires the lock.
    Acquires the lock unless the current thread is interrupted.
    static Lock
    of(Lock delegate)
    Create a new Lock adapter for the given delegate.
  • Method Details

    • of

      static Lock of(Lock delegate)
      Create a new Lock adapter for the given delegate.
      Parameters:
      delegate - must not be null.
      Returns:
      a new Lock adapter.
    • 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

      Lock.AcquiredLock lockInterruptibly() throws InterruptedException
      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,
      then InterruptedException is thrown and the current thread's interrupted status is cleared.
      Throws:
      InterruptedException
    • execute

      default <T> T execute(Supplier<T> action)
      Execute the action specified by the given callback object guarded by a lock and return its result. The action 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

      default void executeWithoutResult(Runnable action)
      Execute the action specified by the given callback object guarded by a lock. The action is only executed once the lock has been acquired.
      Parameters:
      action - the action to run.
      Throws:
      RuntimeException - if thrown by the action.