FileInputStream 和 FileOutputStream 到同一个文件:read() 是否保证看到“之前发生"的所有 write()? [英] FileInputStream and FileOutputStream to the same file: Is a read() guaranteed to see all write()s that "happened before"?

查看:33
本文介绍了FileInputStream 和 FileOutputStream 到同一个文件:read() 是否保证看到“之前发生"的所有 write()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a file as a cache for big data. One thread writes to it sequentially, another thread reads it sequentially.

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

Can I be sure that all data that has been written (by write()) in one thread can be read() from another thread, assuming a proper "happens-before" relationship in terms of the Java memory model? Is this behavior documented?

在我的 JDK 中,FileOutputStream 不会覆盖 flush(),并且 OutputStream.flush() 为空.这就是为什么我想知道...

In my JDK, FileOutputStream does not override flush(), and OutputStream.flush() is empty. That's why I'm wondering...

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

The streams in question are owned exclusively by a class that I have full control of. Each stream is guaranteed to be accesses by one thread only. My tests show that it works as expected, but I'm still wondering if this is guaranteed and documented.

另见此相关讨论.

推荐答案

假设您使用的是 posix 文件系统,那么是的.

Assuming you are using a posix file system, then yes.

FileInputStreamFileOutputStream 在内部使用 read 和 write 系统调用.write 的文档说读取将看到过去写入的结果,

FileInputStream and FileOutputStream on *nix use the read and write system calls internally. The documentation for write says that reads will see the results of past writes,

对常规文件的 write() 成功返回后:

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

从文件中每个字节位置成功的 read()由该写入修改应返回由 write() 指定的数据直到这些字节位置再次被修改.

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() 保证.

I'm pretty sure ntfs on windows will have the same read() write() guarantees.

这篇关于FileInputStream 和 FileOutputStream 到同一个文件:read() 是否保证看到“之前发生"的所有 write()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆