java 多线程访问文件

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

Multithreaded access to file

javamultithreading

提问by ravi

We have a multi threaded java program. Multiple-threads will write to a file, and one thread will read from that file. I am looking for some design ideas. Is synchronization necessary?

我们有一个多线程的java程序。多线程将写入一个文件,一个线程将从该文件中读取。我正在寻找一些设计理念。是否需要同步?

回答by Bill Michell

FileChannelis in theory thread safe. From the javadoc:

FileChannel理论上是线程安全的。从javadoc:

File channels are safe for use by multiple concurrent threads. The close method may be invoked at any time, as specified by the Channel interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified.

多个并发线程可以安全地使用文件通道。close 方法可以在任何时候调用,如 Channel 接口所指定的那样。在任何给定时间,只有一项涉及通道位置或可以更改其文件大小的操作正在进行;在第一个操作仍在进行时尝试启动第二个此类操作将阻塞,直到第一个操作完成。其他操作,特别是那些采取明确立场的操作,可以同时进行;他们是否真的这样做取决于底层实现,因此未指定。

If you can use these, then you can use the built-in synchronization, rather than having to write your own.

如果您可以使用这些,那么您就可以使用内置的同步,而不必自己编写。

回答by bruno conde

I would consider synchronization in this case. Imagine that 2 threads (t1 and t2) open the file at the same time and start writing to it. The changes performed by the first thread are overwrited by the second thread because the second thread is the last to save the changes to the file. When a thread t1 is writing to the file, t2 must wait until t1 finishes it's task before it can open it.

在这种情况下,我会考虑同步。想象一下,2 个线程(t1 和 t2)同时打开文件并开始写入。第一个线程执行的更改会被第二个线程覆盖,因为第二个线程是最后一个将更改保存到文件的线程。当线程 t1 正在写入文件时,t2 必须等到 t1 完成它的任务才能打开它。

Also, if you care about the latest possible update of the file, you should synchronize the writing threads with the thread that reads the file so that if there's any thread writing the the file, the reading thread should wait.

此外,如果您关心文件的最新可能更新,您应该将写入线程与读取文件的线程同步,以便如果有任何线程写入文件,读取线程应该等待。

回答by Robert J. Walker

If being synchronous isn't important, you could have your writer running in its own thread, and allow other threads to queue up writes to the file. Although I think the first thing to consider is whether writing to a file is really what you want to do. Especially in high-traffic situations, having a lot of disk I/O may not be very efficient.

如果同步不重要,您可以让编写器在自己的线程中运行,并允许其他线程将写入文件排队。虽然我认为首先要考虑的是写入文件是否真的是你想要做的。尤其是在高流量情况下,拥有大量磁盘 I/O 可能不是很有效。

回答by Adam Tegen

If you wanted multiple readers and one writer, you would be looking for a Read Write Lockor a Read Write Mutex.

如果你想要多个读者和一个作者,你会寻找读写锁或读写互斥锁。

But you want multiple writers and one reader. How do you know these writers won't overwrite each others data? Are they somehow segregated?

但是您需要多个作者和一个读者。你怎么知道这些作者不会覆盖彼此的数据?他们以某种方式被隔离了吗?

回答by Daniel

Synchronization is necessary in this case. FileChannel is useful for preventing files being modified by processes outside the JVM: not so for applications which include multiple threads writing to a single file. From (further down in) the JavaDoc for FileChannel:

在这种情况下需要同步。FileChannel 可用于防止文件被 JVM 之外的进程修改:对于包含多个线程写入单个文件的应用程序则不然。来自(更深入)FileChannel 的 JavaDoc:

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.

代表整个 Java 虚拟机持有文件锁。它们不适合控制同一虚拟机内的多个线程对文件的访问。

See this postfor a brief discussion of strategies to share file writing between threads.

有关在线程之间共享文件写入的策略的简要讨论,请参阅此帖子

回答by Vincent Ramdhanie

Once multiple Threads access shared data then Synchronization is necessary. If multiple threads write to the same file without some form of locking, then potentially you will end up with a lost update problem.

一旦多个线程访问共享数据,就需要同步。如果多个线程在没有某种形式的锁定的情况下写入同一个文件,那么您可能最终会遇到更新丢失的问题。

Reading is not as big an issue in all circumstances so you need to consider...if a thread is reading the file and at the same time another thread updates the file, does the reading thread need to know about the change? If so you need to lock the file for the reading thread also.

在所有情况下读取都不是一个大问题,因此您需要考虑......如果一个线程正在读取文件,同时另一个线程更新文件,读取线程是否需要知道更改?如果是这样,您还需要为读取线程锁定文件。

回答by user38051

You need synchronization (locking) if you have a mix of readers and writers or writers and writers. If you only have readers, you don't need any synchronization.

如果读者和作者或作者和作者混合使用,则需要同步(锁定)。如果您只有阅读器,则不需要任何同步。

You don't want two processes writing to the same file or one process writing a file that another is reading.

您不希望两个进程写入同一个文件,或者一个进程写入另一个正在读取的文件。