public interface NamedLock extends AutoCloseable
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 } }
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the lock resource.
|
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.
|
String |
name()
Returns this instance name, never null
|
void |
unlock()
Unlocks the lock, must be invoked by caller after one of the
lockShared(long, TimeUnit) or lockExclusively(long, TimeUnit) . |
boolean lockShared(long time, TimeUnit unit) throws InterruptedException
true
.InterruptedException
boolean lockExclusively(long time, TimeUnit unit) throws InterruptedException
true
.InterruptedException
void unlock()
lockShared(long, TimeUnit)
or lockExclusively(long, TimeUnit)
.void close()
unlock()
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 using NamedLockFactory.getLock(String)
. Ideally, instances are to be used
within try-with-resource blocks, so calling this method directly is not really needed, nor advised.close
in interface AutoCloseable
Copyright © 2010–2021 The Apache Software Foundation. All rights reserved.