Class LockRepository

java.lang.Object
com.plotsquared.core.synchronization.LockRepository

public final class LockRepository extends Object
A repository for keyed locks
  • Constructor Details

    • LockRepository

      public LockRepository()
  • Method Details

    • getLock

      public @NonNull Lock getLock(@NonNull LockKey key)
      Get the lock corresponding to the given lock key
      Parameters:
      key - Lock key
      Returns:
      Lock
    • useLock

      public void useLock(@NonNull LockKey key, @NonNull Consumer<Lock> consumer)
      Consume a lock
      Parameters:
      key - Lock key
      consumer - Lock consumer
    • useLock

      public void useLock(@NonNull LockKey key, @NonNull Runnable runnable)
      Wait for the lock to become available, and run the given runnable, then unlock the lock. This is a blocking method.
      Parameters:
      key - Lock key
      runnable - Action to run when the lock is available
    • lock

      Wait for a lock to be available, lock it and return an AutoCloseable instance that locks the key.

      This is meant to be used with try-with-resources, like such:

      
       try (final LockAccess lockAccess = lockRepository.lock(LockKey.of("your.key"))) {
            // use lock
       }
       
      Parameters:
      key - Lock key
      Returns:
      Lock access. Must be closed.