点击上方“朱小厮的博客”,选择“
设为星标”
1. 概览
当读写文件时,需要确保有适当的文件锁定机制,来保证基于并发I/O应用程序的数据完整性。
「本教程中, 我们将介绍使用 Java NIO 库实现这一点的各种方法。」
2. 文件锁简介
「一般来说,有两种锁」
:
简单地说,在写操作完成时,独占锁防止所有其他操作(包括读操作)。
相反,共享锁允许多个进程同时读取。读锁的目的是防止另一个进程获取写锁。通常,处于一致状态的文件确实应该被任何进程读取。
在下一节中,我们将看到Java如何处理这些类型的锁。
3. Java中的文件锁
Java NIO库支持在操作系统级别锁定文件。
FileChannel
中的
lock()
和*tryLock()*方法就是为了这个而存在。
我们可以通过
FileInputStream
,
FileOutputStream
,
RandomAccessFile
来获取
FileChannel
,三者均可通过
getChannel()
方法返回
FileChannel
对象.
或者, 我们可以直接通过静态方法
open
来创建
FileChannel
:
try (FileChannel channel = FileChannel.open(path, openOptions)) {
// write to the channel
}
接下来,我们将回顾在Java中获取独占锁和共享锁的不同方式。要了解有关文件通道的更多信息,请查看[Guide to Java FileChanne 教程。
4. 独占锁
正如我们已经了解到的,在写入文件时,
「我们可以使用独占锁」
防止其他进程读取或写入文件。
我们通过调用
FileChannel
类上的
lock()
或
tryLock())
来获得独占锁。我们还可以使用它们的重载方法:
-
lock(long position, long size, boolean shared)
-
tryLock(long position, long size, boolean shared)
在这些情况下,
shared
参数必须设置为
false
。
要获得独占锁,必须使用可写的
文件通道
。我们可以通过
FileOutputStream
或
RandomAccessFile
的
getChannel()
方法创建它。或者,如前所述,我们可以使用
FileChannel
类的静态方法:
open
。我们只需要将第二个参数设置为
StandardOpenOption.APPEND
:
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.APPEND)) {
// write to channel
}
4.1. 使用
FileOutputStream
的独占锁
从
FileOutputStream
创建的
FileChannel
是可写的。因此,我们可以获得一个独占锁:
try (FileOutputStream fileOutputStream = new FileOutputStream("/tmp/testfile.txt");
FileChannel channel = fileOutputStream.getChannel();
FileLock lock = channel.lock()) {
// write to the channel
}
在这里,
channel.lock()
要么阻塞直到获得一个锁,要么抛出一个异常。例如,如果指定的区域已锁定,则会引发
OverlappingFileLockException
。有关可能的异常的完整列表,请参见Javadoc。我们还可以使用
channel.tryLock()
执行非阻塞锁。如果由于另一个程序持有一个重叠的锁而无法获取锁,则返回
null
。如果由于任何其他原因未能执行此操作,则会引发相应的异常。
4.2. 使用
RandomAccessFile
的独占锁
使用
RandomAccessFile
,我们需要设置 [constructor](https://docs.oracle.com/javase/8/docs/api/java/io/RandomAccessFile.html#RandomAccessFile(java.io.File, java.lang.String)) 方法的第二个参数。
在这里,我们将使用读写权限打开文件:
try (RandomAccessFile file = new RandomAccessFile("/tmp/testfile.txt", "rw");
FileChannel channel = file.getChannel();
FileLock lock = channel.lock()) {
// write to the channel
}
如果我们以只读模式打开文件,并尝试向其通道进行写入操作,将会抛出
NonWritableChannelException
异常。
4.3.独占锁依赖于可读的
FileChannel
如前所述,独占锁需要一个可写通道。因此,我们无法通过从
FileInputStream
创建的
FileChannel
获得独占锁:
Path path = Files.createTempFile("foo","txt");
Logger log = LoggerFactory.getLogger(this.getClass());
try (FileInputStream fis = new FileInputStream(path.toFile());
FileLock lock = fis.getChannel().lock()) {
// unreachable code
} catch (NonWritableChannelException e) {
// handle exception
}
在上面的例子中,
lock()
方法将抛出一个
nonwriteablechannelexception
。实际上,这是因为我们正在对一个创建只读通道的
FileInputStream
调用
getChannel
。这个例子只是为了证明我们不能写到一个不可写的通道。事实上,我们不会捕捉并重新抛出异常。
5. 共享锁
记住,共享锁也称为
读
锁。因此,要获得读锁,我们必须使用可读的
文件通道
。
这样的
FileChannel
可以通过调用
FileInputStream
或
RandomAccessFile
上的
getChannel()
方法获得。同样,另一个选项是使用
FileChannel
类的静态
open
方法。在这种情况下,我们将第二个参数设置为
StandardOpenOption.READ
。
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ);
FileLock lock = channel.lock(0, Long.MAX_VALUE, true)) {
// read from the channel
}
这里要注意的一点是,我们选择通过调用
lock(0, Long.MAX_VALUE, true)
来锁定整个文件。通过将前两个参数更改为不同的值,我们还可以只锁定文件的特定区域。对于共享锁,第三个参数必须设置为
true
。
为了简单起见,我们将在下面的所有示例中锁定整个文件,但请记住,我们始终可以锁定文件的特定区域。
5.1. 使用
FileInputStream
中的共享锁
从
FileInputStream
获得的
FileChannel
是可读的。因此,我们可以获得一个共享锁:
try (FileInputStream fileInputStream = new FileInputStream("/tmp/testfile.txt");
FileChannel channel = fileInputStream.getChannel();
FileLock lock = channel.lock(0, Long.MAX_VALUE, true)) {
// read from the channel
}
在上面的代码片段中,将成功调用通道上的
lock()
。这是因为共享锁只要求通道是可读的就行。
5.2. 使用
RandomAccessFile
中的共享锁
这次,我们只需要使用 ''读" 权限打开文件即可:
try (RandomAccessFile file = new RandomAccessFile("/tmp/testfile.txt", "r");
FileChannel channel = file.getChannel();
FileLock lock = channel.lock(0, Long.MAX_VALUE, true)) {
// read from the channel
}
在本例中,我们创建了一个具有读取权限的
RandomAccessFile
对象,然后从中创建一个可读通道,从而创建一个共享锁。
5.3. 共享锁依赖于可读的
FileChannel
因此,我们无法通过从
FileOutputStream
创建的
FileChannel
获取共享锁:
Path path = Files.createTempFile("foo","txt");
try (FileOutputStream fis = new FileOutputStream(path.toFile());
FileLock lock = fis.getChannel().lock(0, Long.MAX_VALUE, true)) {
// unreachable code
} catch (NonWritableChannelException e) {
// handle exception
}
在本例中,调用
lock()