Class GLock

java.lang.Object
com.glitchybyte.glib.concurrent.GLock

public final class GLock extends Object
Lock utilities.

This utility class contains common locking constructs.

These utilities are not meant to replace all use cases. They encapsulate common quick operations. They are especially unsuited for operations that throw exceptions.

  • Method Details

    • locked

      public static void locked(Lock lock, Runnable runnable)
      Run a block of code between lock and unlock.
      Parameters:
      lock - Lock.
      runnable - Block of code.
    • lockedResult

      public static <V> V lockedResult(Lock lock, Supplier<V> supplier)
      Run a block of code, with a return value, between lock and unlock.
      Type Parameters:
      V - Type of the return value.
      Parameters:
      lock - Lock.
      supplier - Block of code.
      Returns:
      Value returned by the block.
    • awaitCondition

      public static void awaitCondition(Lock lock, Condition condition) throws InterruptedException
      Acquire lock and awaits on the condition.
      Parameters:
      lock - Lock.
      condition - Condition.
      Throws:
      InterruptedException - If interrupted while awaiting.
    • awaitConditionWithTest

      public static void awaitConditionWithTest(Lock lock, Condition condition, BooleanSupplier test) throws InterruptedException
      Acquire lock and awaits on the condition until test is true.
      Parameters:
      lock - Lock.
      condition - Condition.
      test - Test to await for.
      Throws:
      InterruptedException - If interrupted while awaiting.
    • awaitConditionWithTimeout

      public static boolean awaitConditionWithTimeout(Lock lock, Condition condition, Duration timeout) throws InterruptedException
      Acquire lock and awaits on the condition up to the given timeout.
      Parameters:
      lock - Lock.
      condition - Condition.
      timeout - Await timeout.
      Returns:
      True if condition was signaled. False if await timed out.
      Throws:
      InterruptedException - If interrupted while awaiting.
    • awaitConditionWithTestAndTimeout

      public static boolean awaitConditionWithTestAndTimeout(Lock lock, Condition condition, BooleanSupplier test, Duration timeout) throws InterruptedException
      Acquire lock and awaits on the condition until test is true or timeout expires.
      Parameters:
      lock - Lock.
      condition - Condition.
      test - Test to await for.
      timeout - Await timeout.
      Returns:
      True if test is true.
      Throws:
      InterruptedException - If interrupted while awaiting.
    • signalAll

      public static void signalAll(Lock lock, Condition condition)
      Signal all monitors on the given condition.
      Parameters:
      lock - Lock.
      condition - Condition.
    • readLocked

      public static void readLocked(ReadWriteLock lock, Runnable runnable)
      Run a block of code between a read-lock and unlock.
      Parameters:
      lock - Read/write lock.
      runnable - Block of code.
    • readLockedResult

      public static <V> V readLockedResult(ReadWriteLock lock, Supplier<V> supplier)
      Run a block of code, with a return value, between a read-lock and unlock.
      Type Parameters:
      V - Type of the return value.
      Parameters:
      lock - Lock.
      supplier - Block of code.
      Returns:
      Value returned by the block.
    • writeLocked

      public static void writeLocked(ReadWriteLock lock, Runnable runnable)
      Run a block of code between a write-lock and unlock.
      Parameters:
      lock - Read/write lock.
      runnable - Block of code.
    • writeLockedResult

      public static <V> V writeLockedResult(ReadWriteLock lock, Supplier<V> supplier)
      Run a block of code, with a return value, between a write-lock and unlock.
      Type Parameters:
      V - Type of the return value.
      Parameters:
      lock - Lock.
      supplier - Block of code.
      Returns:
      Value returned by the block.