Package io.ocfl.core
Class FileLocker
- java.lang.Object
-
- io.ocfl.core.FileLocker
-
public class FileLocker extends Object
Provides locks for logical paths, so that an object may be safely modified by multiple threads.
-
-
Constructor Summary
Constructors Constructor Description FileLocker(Duration timeoutDuration)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ReentrantLock
lock(String logicalPath)
Returns a lock on the specified logical path or throws aLockException
if a lock was unable to be acquired.<T> T
withLock(String logicalPath, UncheckedCallable<T> callable)
Executes the callable after acquire a lock on the specified logical path.void
withLock(String logicalPath, Runnable runnable)
Executes the runnable after acquire a lock on the specified logical path.
-
-
-
Constructor Detail
-
FileLocker
public FileLocker(Duration timeoutDuration)
- Parameters:
timeoutDuration
- the max amount of time to wait for a file lock
-
-
Method Detail
-
lock
public ReentrantLock lock(String logicalPath)
Returns a lock on the specified logical path or throws aLockException
if a lock was unable to be acquired. This lock MUST be released in a finally block.- Parameters:
logicalPath
- the path to lock- Returns:
- the lock
- Throws:
LockException
- when unable to acquire a lock
-
withLock
public void withLock(String logicalPath, Runnable runnable)
Executes the runnable after acquire a lock on the specified logical path. If the lock cannot be acquired, aLockException
is thrown.- Parameters:
logicalPath
- the path to lock- Throws:
LockException
- when unable to acquire a lock
-
withLock
public <T> T withLock(String logicalPath, UncheckedCallable<T> callable)
Executes the callable after acquire a lock on the specified logical path. If the lock cannot be acquired, aLockException
is thrown.- Parameters:
logicalPath
- the path to lock- Returns:
- the output of the callable
- Throws:
LockException
- when unable to acquire a lock
-
-