java - FileInputStream 和 FileOutputStream 到同一个文件 : Is a read() guaranteed to see all write()s that "happened before"?

标签 java concurrency stream

我正在使用一个文件作为大数据的缓存。一个线程顺序写入它,另一个线程顺序读取它。

我能否确定在一个线程中(通过write())写入的所有数据都可以从另一个线程中被read(),假设适当的“在 Java 内存模型方面发生“之前”的关系?这种行为是否记录在案?

在我的JDK中,FileOutputStream没有覆盖flush()OutputStream.flush()是空的。这就是为什么我想知道...

有问题的流完全由我完全控制的类拥有。每个流都保证只能由一个线程访问。我的测试表明它按预期工作,但我仍然想知道这是否得到保证和记录。

另见 this related discussion .

最佳答案

假设您使用的是 posix 文件系统,那么可以。

*nix 上的

FileInputStreamFileOutputStream 在内部使用读写系统调用。 write 的文档说 reads will see the results of past writes ,

After a write() to a regular file has successfully returned:

Any successful read() from each byte position in the file that was modified by that write shall return the data specified by the write() for that position until such byte positions are again modified.

我很确定 Windows 上的 ntfs 将具有相同的 read() write() 保证。

关于java - FileInputStream 和 FileOutputStream 到同一个文件 : Is a read() guaranteed to see all write()s that "happened before"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54889316/

相关文章:

java - 使用 Java8 Stream 将复杂数据映射到对象

java - 在 Java 中使用 PrintWriter 创建文件并写入该文件

java - 使用 ScheduledThreadPool 并行执行

java - 是否可以使用spark-avro数据源创建Spark应用程序并通过 'java -jar'执行它

java - 创建类似于java对象层次结构的树

java - 使用java和mysql服务器的读/写模式

c++ - 从文件段错误(核心转储)中读取字符串

java - ActiveMQ 向 StompConnection 注册监听器

java - 显示 JWindow 一段时间

java - 如何在java中摆脱这个 "static method should be acessed in a static way"?