Class SimpleLock
- All Implemented Interfaces:
AutoCloseable
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:
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:
SimpleReadWriteLock- A read-write lock that returnsSimpleLockinstancesLock- The underlying lock interface
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final SimpleLockA no-op lock instance that performs no actual locking operations. -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
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
SimpleLockinstance 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.
- Calling
-
-
Constructor Details
-
SimpleLock
Constructor.Creates a new
SimpleLockwrapper around the specifiedLock. The lock is automatically acquired during construction. Ifnull 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 andclose()will have no effect. - The underlying lock's behavior (fairness, reentrancy, etc.) is preserved.
- Parameters:
lock- TheLockbeing wrapped. Can benull to create a no-op lock.
-
-
Method Details
-
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:
closein interfaceAutoCloseable- Throws:
IllegalMonitorStateException- If the current thread does not hold this lock (only applies to certain lock implementations).
-