Java 文件锁定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2045734/
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-10-29 19:06:55  来源:igfitidea点击:

Java File Locking

javalocking

提问by Jo?o Silva

I have several threads (some of which are spawned by Process X, others by Process Y, et cetera), and each thread needs to write to a file MyFile. However, if Thread T1starts writing to MyFilefirst, then, when Thread T2starts writing, it needs to wait for T1to release the file, so that it can read the contents that were written in Thread T1. In other words, each thread would have a finalizeThreadmethod, like so:

我有几个线程(其中一些由 Process X 产生,其他由 Process Y 产生,等等),并且每个线程都需要写入一个文件MyFile。但是,如果Thread T1开始写MyFile第一,那么,当Thread T2开始写作,它需要等待T1释放文件,以便它可以读取写在该内容Thread T1。换句话说,每个线程都有一个finalizeThread方法,如下所示:

private void finalizeThread() {
    File f = new File("MyFile.dat");
    f.createNewFile();  // atomically creates the file, if it doesn't exist
    locked_section {
        readContentsFromFile(f); // read contents if some other thread already modified the file
        modifyContentsFromFile(f); // modify
        writeFile(f); // write, so that new threads can see the content modified by this thread
    }
}

My question is: How can I accomplish the locked_sectionin the above code? I was looking into the FileLockclass, but it says in the Javadoc that "File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.".

我的问题是:我怎样才能完成locked_section上面的代码?我正在研究这个FileLock类,但它在 Javadoc 中说“文件锁是代表整个 Java 虚拟机持有的。它们不适合控制同一虚拟机内的多个线程对文件的访问。” .

采纳答案by AndiDog

If the file is only accessed from your program, the synchronized lock object is okay. But if you want to protect the file from being changed by other programs while you're working on it, you can use Java's file locking features in java.nio.channels.FileLock(example). As the text says, mind that on some operating systems, programs can still change files if they don't check for an existing file lock.

如果该文件只能从您的程序中访问,则同步锁定对象是可以的。但是,如果您想在处理文件时保护文件不被其他程序更改,则可以使用java.nio.channels.FileLock示例)中的Java 文件锁定功能。正如文中所说,请注意,在某些操作系统上,如果程序不检查现有文件锁,它们仍然可以更改文件。

回答by danben

Instead of sharing a lock, maybe you could have a separate process that was responsible for maintaining a lock on the file. To begin your read/modify/write step, a Thread would have to ask this central process for the lock via HTTP, or messaging, or whatever you like. If the request is denied, the Thread would go to sleep, wake up, and try again. Otherwise the Thread would read/modify/write and then tell the locking process that it is releasing the lock.

与其共享锁,也许您可​​以有一个单独的进程来负责维护文件的锁。要开始你的读/修改/写步骤,线程必须通过 HTTP、消息传递或任何你喜欢的方式向这个中央进程请求锁定。如果请求被拒绝,线程将进入睡眠状态,唤醒,然后再试一次。否则线程将读取/修改/写入,然后告诉锁定进程它正在释放锁定。

回答by Anon.

You'll want to synchronize on some object. For example:

您需要在某个对象上进行同步。例如:

synchronized(fileLockObject) {
    readContentsFromFile(f);
    modifyContentsFromFile(f);
    writeFile(f);
}