文件锁是否有可能永远不会被释放?
Is it possible that a file lock never gets released?
给定以下文件锁定请求:
FileLock lock = null;
try {
lock = randomAccessFile.getChannel().lock(0, Long.MAX_VALUE, mode.shared);
// work with file
} finally {
if (lock != null) {
lock.release();
}
}
目标 OS 是 MS Windows,是否有可能 finally
块永远不会执行,因此锁永远不会释放?例如,如果 JVM 崩溃了怎么办?如何处理这种无所有者锁?
当进程退出时,任何 OS 自动释放进程获取的所有资源,但无法保证何时会发生。
如果 Windows Oracle JVM 使用 LockFileEx function as a native implementation and according to msdn https://msdn.microsoft.com/en-us/library/aa365202.aspx
If a process terminates with a portion of a file locked or closes a file that has outstanding locks, the locks are unlocked by the operating system. However, the time it takes for the operating system to unlock these locks depends upon available system resources. Therefore, it is recommended that your process explicitly unlock all files it has locked when it terminates. If this is not done, access to these files may be denied if the operating system has not yet unlocked them.
给定以下文件锁定请求:
FileLock lock = null;
try {
lock = randomAccessFile.getChannel().lock(0, Long.MAX_VALUE, mode.shared);
// work with file
} finally {
if (lock != null) {
lock.release();
}
}
目标 OS 是 MS Windows,是否有可能 finally
块永远不会执行,因此锁永远不会释放?例如,如果 JVM 崩溃了怎么办?如何处理这种无所有者锁?
当进程退出时,任何 OS 自动释放进程获取的所有资源,但无法保证何时会发生。
如果 Windows Oracle JVM 使用 LockFileEx function as a native implementation and according to msdn https://msdn.microsoft.com/en-us/library/aa365202.aspx
If a process terminates with a portion of a file locked or closes a file that has outstanding locks, the locks are unlocked by the operating system. However, the time it takes for the operating system to unlock these locks depends upon available system resources. Therefore, it is recommended that your process explicitly unlock all files it has locked when it terminates. If this is not done, access to these files may be denied if the operating system has not yet unlocked them.