用于读写的 Java FileLock

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2268997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:29:13  来源:igfitidea点击:

Java FileLock for Reading and Writing

javafile-lockingfilelock

提问by bobtheowl2

I have a process that will be called rather frequently from cron to read a file that has certain move related commands in it. My process needs to read and write to this data file - and keep it locked to prevent other processes from touching it during this time. A completely separate process can be executed by a user to (potential) write/append to this same data file. I want these two processes to play nice and only access the file one at a time.

我有一个进程会被 cron 频繁调用以读取其中包含某些与移动相关的命令的文件。我的进程需要读取和写入此数据文件 - 并保持锁定状态以防止其他进程在此期间接触它。用户可以执行一个完全独立的过程来(可能)写入/追加到同一数据文件。我希望这两个进程运行良好,并且一次只访问一个文件。

The nio FileLock seemed to be what I needed (short of writing my own semaphore type files), but I'm having trouble locking it for reading. I can lock and write just fine, but when attempting to create lock when reading I get a NonWritableChannelException. Is it even possible to lock a file for reading? Seems like a RandomAccessFile is closer to what I need, but I don't see how to implement that.

nio FileLock 似乎是我所需要的(没有编写我自己的信号量类型文件),但是我无法锁定它以进行读取。我可以很好地锁定和写入,但是在读取时尝试创建锁定时,我得到了一个 NonWritableChannelException。甚至可以锁定文件以供阅读吗?似乎 RandomAccessFile 更接近我需要的东西,但我不知道如何实现它。

Here is the code that fails:

这是失败的代码:

FileInputStream fin = new FileInputStream(f);
FileLock fl = fin.getChannel().tryLock();
if(fl != null) 
{
  System.out.println("Locked File");
  BufferedReader in = new BufferedReader(new InputStreamReader(fin));
  System.out.println(in.readLine());
          ...

The exception is thrown on the FileLock line.

在 FileLock 行上抛出异常。

java.nio.channels.NonWritableChannelException
 at sun.nio.ch.FileChannelImpl.tryLock(Unknown Source)
 at java.nio.channels.FileChannel.tryLock(Unknown Source)
 at Mover.run(Mover.java:74)
 at java.lang.Thread.run(Unknown Source)

Looking at the JavaDocs, it says

查看 JavaDocs,它说

Unchecked exception thrown when an attempt is made to write to a channel that was not originally opened for writing.

尝试写入最初未打开用于写入的通道时引发未经检查的异常。

But I don't necessarily need to write to it. When I try creating a FileOutpuStream, etc. for writing purposes it is happy until I try to open a FileInputStream on the same file.

但我不一定需要写信给它。当我尝试创建 FileOutpuStream 等用于编写目的时,它很高兴,直到我尝试在同一文件上打开 FileInputStream。

采纳答案by user207421

(a) Are you aware that locking the file won't keep other processes from touching it unless they also use locks?
(b) You have to lock via a writable channel. Get the lock via a RandomAccessFilein "rw" mode and then open your FileInputStream. Make sure to close both!

(a) 您是否知道锁定文件不会阻止其他进程接触它,除非它们也使用锁?
(b) 您必须通过可写通道锁定。通过RandomAccessFile以“rw”模式获取锁,然后打开您的FileInputStream. 一定要关闭两个!

回答by Huxi

It would be better if you created the lock using tryLock(0L, Long.MAX_VALUE, true).

如果您使用tryLock(0L, Long.MAX_VALUE, true).

This creates a shared lock which is the right thing to do for reading.

这会创建一个共享锁,这是阅读的正确做法。

tryLock()is a shorthand for tryLock(0L, Long.MAX_VALUE, false), i.e. it requests an exclusive write-lock.

tryLock()是 的简写tryLock(0L, Long.MAX_VALUE, false),即它请求独占写锁。

回答by Davi Cavalcanti

I wrote a test program and bash commands to confirm the effectivity of the file lock:

我写了一个测试程序和bash命令来确认文件锁的有效性:

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;

public class FileWriterTest
{
    public static void main(String[] args) throws Exception
    {
        if (args.length != 4)
        {
            System.out.println("Usage: FileWriterTest <filename> <string> <sleep ms> <enable lock>");
            System.exit(1);
        }

        String filename = args[0];
        String data = args[1];
        int sleep = Integer.parseInt(args[2]);
        boolean enableLock = Boolean.parseBoolean(args[3]);

        try (RandomAccessFile raFile = new RandomAccessFile(new File(filename), "rw"))
        {
            FileLock lock = null;
            if (enableLock)
            {
                lock = raFile.getChannel().lock();
            }

            Thread.sleep(sleep);
            raFile.seek(raFile.length());
            System.out.println("writing " + data + " in a new line; current pointer = " + raFile.getFilePointer());
            raFile.write((data+"\n").getBytes());

            if (lock != null)
            {
                lock.release();
            }
        }
    }
}

Run with this bash command to check it works:

使用此 bash 命令运行以检查它是否有效:

for i in {1..1000}
do
java FileWriterTest test.txt $i 10 true &
done

You should see the writing only happening once every 10ms (from the outputs), and in the end all numbers to be present in the file.

您应该看到每 10 毫秒写入一次(来自输出),最后所有数字都出现在文件中。

Output:

输出:

/tmp wc -l test.txt
1000 test.txt
/tmp

The same test without the lock shows data being lost:

没有锁定的相同测试显示数据丢失:

for i in {1..1000}
do
java FileWriterTest test.txt $i 10 false &
done

Output:

输出:

/tmp wc -l test.txt
764 test.txt
/tmp

It should be easy to modify it to test the tryLock instead.

修改它来测试 tryLock 应该很容易。