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

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

问题描述

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

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()为空。这就是为什么我想知道......

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

EDIT ^ 2 :有问题的流程由我完全控制的类独家拥有的。保证每个流只能由一个线程访问。我的测试显示它按预期工作,但我仍然想知道这是否有保证和记录。

EDIT^2: 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.

参见这个相关的讨论:http://chat.stackoverflow.com/rooms/17598/discussion-between-hussain-al-mutawa -and-user946850

See also this related discussion: http://chat.stackoverflow.com/rooms/17598/discussion-between-hussain-al-mutawa-and-user946850

推荐答案

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

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

* nix上的FileInputStream和FileOutputStream在内部使用读写系统调用。写的文档说读取将看到过去写入的结果

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()s“发生在之前”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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