Class SimpleLock

java.lang.Object
org.apache.juneau.commons.concurrent.SimpleLock
All Implemented Interfaces:
AutoCloseable

public class SimpleLock extends Object implements AutoCloseable
A simple auto-closeable wrapper around a Lock that automatically acquires the lock when created and releases it when closed.

This class wraps any Lock implementation and makes it work seamlessly with try-with-resources statements. The lock is automatically acquired in the constructor and released when close() is called (either explicitly or automatically via try-with-resources).

This class is typically used indirectly through SimpleReadWriteLock.read() and SimpleReadWriteLock.write(), but can also be used directly with any Lock instance.

Example:

// Using with a ReentrantLock var lock = new ReentrantLock(); try (var simpleLock = new SimpleLock(lock)) { // Lock is automatically held - perform critical section operations sharedResource.modify(); } // Lock is automatically released when exiting try block // Using with SimpleReadWriteLock var rwLock = new SimpleReadWriteLock(); try (var readLock = rwLock.read()) { // Read lock is automatically held sharedResource.read(); } // Read lock is automatically released

Thread Safety:

This class is not thread-safe itself. Each instance wraps a single Lock and should be used by a single thread. The underlying lock's thread-safety guarantees apply to the wrapped lock.

Notes:
  • The lock is automatically acquired in the constructor and released in close().
  • If null is passed to the constructor, no locking operations are performed (useful for creating no-op locks).
  • This class implements AutoCloseable, making it ideal for use with try-with-resources statements.
  • Multiple calls to close() are safe - subsequent calls have no effect if the lock has already been released.
  • The underlying lock's reentrancy behavior is preserved - if the lock supports reentrant locking, the same thread can acquire it multiple times.
See Also:
  • Field Details

    • NO_OP

      public static final SimpleLock NO_OP
      A no-op lock instance that performs no actual locking operations.

      This instance can be used when locking is conditionally disabled or when you need a SimpleLock instance but don't want any actual synchronization overhead. All lock operations on this instance are no-ops.

      Example:

      // Use NO_OP when locking is disabled var lock = isLockingEnabled() ? new SimpleLock(new ReentrantLock()) : SimpleLock.NO_OP; try (var simpleLock = lock) { // Code works the same whether locking is enabled or not sharedResource.modify(); }

      Notes:
      • Calling close() on this instance has no effect.
      • This instance is thread-safe and can be shared across threads.
      • This instance wraps a null lock, so no actual locking occurs.
  • Constructor Details

    • SimpleLock

      public SimpleLock(Lock lock)
      Constructor.

      Creates a new SimpleLock wrapper around the specified Lock. The lock is automatically acquired during construction. If null is passed, no locking operations are performed (useful for creating no-op locks).

      Example:

      // Wrap a ReentrantLock var underlyingLock = new ReentrantLock(); var simpleLock = new SimpleLock(underlyingLock); // Lock is now held - use try-with-resources to ensure release try (simpleLock) { // Critical section }

      Notes:
      • The lock is acquired immediately in the constructor. This method may block if the lock is not available.
      • If null is passed, no lock is acquired and close() will have no effect.
      • The underlying lock's behavior (fairness, reentrancy, etc.) is preserved.
      Parameters:
      lock - The Lock being wrapped. Can be null to create a no-op lock.
  • Method Details

    • close

      public void close()
      Releases the lock.

      This method is called automatically when used in a try-with-resources statement, or can be called explicitly to release the lock early. If the lock was null (no-op lock), this method has no effect.

      Example:

      var lock = new SimpleLock(new ReentrantLock()); // Lock is held lock.close(); // Explicitly release the lock

      Notes:
      • This method is called automatically when exiting a try-with-resources block.
      • Multiple calls to this method are safe - subsequent calls have no effect if the lock has already been released.
      • If the lock was null (no-op lock), this method returns immediately without performing any operations.
      • The underlying lock's unlock behavior is preserved (e.g., reentrant locks must be unlocked the same number of times they were locked).
      Specified by:
      close in interface AutoCloseable
      Throws:
      IllegalMonitorStateException - If the current thread does not hold this lock (only applies to certain lock implementations).