Package org.eclipse.aether.named
Interface NamedLock
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
AdaptedSemaphoreNamedLock
,CompositeNamedLock
,FileLockNamedLock
,NamedLockSupport
,ReadWriteLockNamedLock
A named lock, functionally similar to existing JVM and other implementations. Must support boxing (reentrancy), but
no lock upgrade is supported. The lock instance obtained from lock factory must be treated as a resource, best in
try-with-resource block. Usual pattern to use this lock:
try (NamedLock lock = factory.getLock("resourceName")) { if (lock.lockExclusively(10L, Timeunit.SECONDS)) { try { ... exclusive access to "resourceName" resource gained here } finally { lock.unlock(); } } else { ... failed to gain access within specified time, handle it } }
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the lock resource.key()
Returns this instance key, nevernull
.boolean
lockExclusively
(long time, TimeUnit unit) Tries to lock exclusively, may block for given time.boolean
lockShared
(long time, TimeUnit unit) Tries to lock shared, may block for given time.void
unlock()
Unlocks the lock, must be invoked by caller after one of thelockShared(long, TimeUnit)
orlockExclusively(long, TimeUnit)
.
-
Method Details
-
key
Returns this instance key, nevernull
. -
lockExclusively
Tries to lock exclusively, may block for given time. If successful, returnstrue
.- Throws:
InterruptedException
-
unlock
void unlock()Unlocks the lock, must be invoked by caller after one of thelockShared(long, TimeUnit)
orlockExclusively(long, TimeUnit)
. -
close
void close()Closes the lock resource. Lock MUST be unlocked usingunlock()
in case any locking happened on it. After invoking this method, the lock instance MUST NOT be used anymore. If lock for same name needed, a new instance should be obtained from factory usingNamedLockFactory.getLock(java.util.Collection)
. Ideally, instances are to be used within try-with-resource blocks, so calling this method directly is not really needed, nor advised.- Specified by:
close
in interfaceAutoCloseable
-