Package org.apache.lucene.store
Class Lock
java.lang.Object
org.apache.lucene.store.Lock
- All Implemented Interfaces:
Closeable
,AutoCloseable
An interprocess mutex lock.
Typical use might look like:
new Lock.With(directory.makeLock("my.lock")) { public Object doBody() { ... code to execute while locked ... } }.run();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Utility class for executing code with exclusive access. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Throwable
If a lock obtain called, this failureReason may be set with the "root cause" Exception as to why the lock was not obtained.static final long
Pass this value toobtain(long)
to try forever to obtain the lock.static long
How longobtain(long)
waits, in milliseconds, in between attempts to acquire the lock. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract void
close()
Releases exclusive access.abstract boolean
isLocked()
Returns true if the resource is currently locked.abstract boolean
obtain()
Attempts to obtain exclusive access and immediately return upon success or failure.boolean
obtain
(long lockWaitTimeout) Attempts to obtain an exclusive lock within amount of time given.
-
Field Details
-
LOCK_POLL_INTERVAL
public static long LOCK_POLL_INTERVALHow longobtain(long)
waits, in milliseconds, in between attempts to acquire the lock. -
LOCK_OBTAIN_WAIT_FOREVER
public static final long LOCK_OBTAIN_WAIT_FOREVERPass this value toobtain(long)
to try forever to obtain the lock.- See Also:
-
failureReason
If a lock obtain called, this failureReason may be set with the "root cause" Exception as to why the lock was not obtained.
-
-
Constructor Details
-
Lock
public Lock()
-
-
Method Details
-
obtain
Attempts to obtain exclusive access and immediately return upon success or failure. Useclose()
to release the lock.- Returns:
- true iff exclusive access is obtained
- Throws:
IOException
-
obtain
Attempts to obtain an exclusive lock within amount of time given. Polls once perLOCK_POLL_INTERVAL
(currently 1000) milliseconds until lockWaitTimeout is passed.- Parameters:
lockWaitTimeout
- length of time to wait in milliseconds orLOCK_OBTAIN_WAIT_FOREVER
to retry forever- Returns:
- true if lock was obtained
- Throws:
LockObtainFailedException
- if lock wait times outIllegalArgumentException
- if lockWaitTimeout is out of boundsIOException
- if obtain() throws IOException
-
close
Releases exclusive access.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
isLocked
Returns true if the resource is currently locked. Note that one must still callobtain()
before using the resource.- Throws:
IOException
-