Class OpenFileLock
java.lang.Object
cloud.opencode.base.io.lock.OpenFileLock
File Locking Utility Class
文件锁定工具类
Provides file locking utilities for cross-process synchronization using NIO FileChannel locks.
提供基于 NIO FileChannel 锁的跨进程同步文件锁定工具。
Features | 主要功能:
- Exclusive and shared locks - 独占锁和共享锁
- Try-lock with timeout - 带超时的尝试锁定
- Lock file pattern for coordination - 锁文件模式用于协调
- AutoCloseable for try-with-resources - AutoCloseable 支持 try-with-resources
Usage Examples | 使用示例:
// Exclusive lock with try-with-resources
try (var lock = OpenFileLock.lock(path)) {
// Exclusive access to file
}
// Shared (read) lock
try (var lock = OpenFileLock.lockShared(path)) {
// Shared read access
}
// Try lock with timeout
Optional<FileLockHandle> lock = OpenFileLock.tryLock(path, Duration.ofSeconds(5));
if (lock.isPresent()) {
try (var handle = lock.get()) {
// Got the lock
}
}
// Execute with lock
String result = OpenFileLock.withLock(path, () -> {
return readAndProcessFile(path);
});
Security | 安全性:
- Thread-safe: Yes, uses NIO FileChannel locks for cross-process safety - 线程安全: 是,使用NIO FileChannel锁实现跨进程安全
- Null-safe: No, path must not be null - 空值安全: 否,路径不可为null
- Since:
- JDK 25, opencode-base-io V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic PathgetLockFilePath(Path path) Get lock file path for a resource 获取资源的锁文件路径static booleanisExclusivelyLocked(Path path) Check if file is locked with shared lock 检查文件是否被共享锁定static booleanCheck if file is locked 检查文件是否被锁定static OpenFileLock.FileLockHandleAcquire exclusive lock on file 获取文件的独占锁static OpenFileLock.FileLockHandleAcquire lock on file 获取文件锁static OpenFileLock.FileLockHandleCreate a lock file for coordination 创建用于协调的锁文件static OpenFileLock.FileLockHandlelockShared(Path path) Acquire shared (read) lock on file 获取文件的共享(读)锁static Optional<OpenFileLock.FileLockHandle> Try to acquire exclusive lock without blocking 尝试获取独占锁(不阻塞)static Optional<OpenFileLock.FileLockHandle> Try to acquire lock without blocking 尝试获取锁(不阻塞)static Optional<OpenFileLock.FileLockHandle> Try to acquire lock with timeout 尝试在超时时间内获取锁static Optional<OpenFileLock.FileLockHandle> Try to acquire lock with timeout 尝试在超时时间内获取锁static Optional<OpenFileLock.FileLockHandle> tryLockFile(Path path, Duration timeout) Try to create a lock file with timeout 尝试在超时时间内创建锁文件static <T> Optional<T> tryWithLock(Path path, Duration timeout, Supplier<T> action) Try to execute action with lock, with timeout 尝试在超时时间内持有锁执行操作static voidExecute action with exclusive lock (no return value) 持有独占锁执行操作(无返回值)static <T> TExecute action with exclusive lock 持有独占锁执行操作static <T> TwithSharedLock(Path path, Supplier<T> action) Execute action with shared lock 持有共享锁执行操作
-
Field Details
-
LOCK_FILE_SUFFIX
-
-
Method Details
-
lock
Acquire exclusive lock on file 获取文件的独占锁- Parameters:
path- the file path | 文件路径- Returns:
- lock handle | 锁句柄
- Throws:
OpenIOOperationException- if lock cannot be acquired | 如果无法获取锁
-
lock
Acquire lock on file 获取文件锁- Parameters:
path- the file path | 文件路径shared- true for shared lock, false for exclusive | true 为共享锁,false 为独占锁- Returns:
- lock handle | 锁句柄
- Throws:
OpenIOOperationException- if lock cannot be acquired | 如果无法获取锁
-
tryLock
Try to acquire exclusive lock without blocking 尝试获取独占锁(不阻塞)- Parameters:
path- the file path | 文件路径- Returns:
- lock handle if acquired, empty otherwise | 如果获取成功返回锁句柄,否则返回空
-
tryLock
Try to acquire lock without blocking 尝试获取锁(不阻塞)- Parameters:
path- the file path | 文件路径shared- true for shared lock | true 为共享锁- Returns:
- lock handle if acquired, empty otherwise | 如果获取成功返回锁句柄,否则返回空
-
tryLock
Try to acquire lock with timeout 尝试在超时时间内获取锁- Parameters:
path- the file path | 文件路径timeout- maximum wait time | 最大等待时间- Returns:
- lock handle if acquired, empty otherwise | 如果获取成功返回锁句柄,否则返回空
-
tryLock
public static Optional<OpenFileLock.FileLockHandle> tryLock(Path path, Duration timeout, boolean shared) Try to acquire lock with timeout 尝试在超时时间内获取锁- Parameters:
path- the file path | 文件路径timeout- maximum wait time | 最大等待时间shared- true for shared lock | true 为共享锁- Returns:
- lock handle if acquired, empty otherwise | 如果获取成功返回锁句柄,否则返回空
-
lockFile
Create a lock file for coordination 创建用于协调的锁文件- Parameters:
path- the resource path to protect | 要保护的资源路径- Returns:
- lock handle for the lock file | 锁文件的锁句柄
-
tryLockFile
Try to create a lock file with timeout 尝试在超时时间内创建锁文件- Parameters:
path- the resource path to protect | 要保护的资源路径timeout- maximum wait time | 最大等待时间- Returns:
- lock handle if acquired, empty otherwise | 如果获取成功返回锁句柄,否则返回空
-
getLockFilePath
-
withLock
-
withLock
-
tryWithLock
Try to execute action with lock, with timeout 尝试在超时时间内持有锁执行操作- Type Parameters:
T- return type | 返回类型- Parameters:
path- the file path | 文件路径timeout- maximum wait time | 最大等待时间action- action to execute | 要执行的操作- Returns:
- action result if lock acquired, empty otherwise | 如果获取锁返回结果,否则返回空
-
isLocked
Check if file is locked 检查文件是否被锁定- Parameters:
path- the file path | 文件路径- Returns:
- true if locked by another process | 如果被其他进程锁定返回 true
-
isExclusivelyLocked
Check if file is locked with shared lock 检查文件是否被共享锁定- Parameters:
path- the file path | 文件路径- Returns:
- true if exclusively locked | 如果被独占锁定返回 true
-