简体   繁体   English

是否需要FileChannel.force和FileDescriptor.sync?

[英]Are FileChannel.force and FileDescriptor.sync both needed?

In https://stackoverflow.com/questions/730521/really-force-file-sync-flush-in-java , the author writes in the summary of the answers: https://stackoverflow.com/questions/730521/really-force-file-sync-flush-in-java中 ,作者在答案摘要中写道:

Use c.force(true) followed by s.getFD().sync() for Java NIO 对于Java NIO,使用c.force(true),然后使用s.getFD()。sync()

My question is: do you really need both? 我的问题是:你真的需要两者吗? Isn't force enough? 力不够吗? Aren't force and sync just different interfaces for doing the same thing? 是不是强制和同步只是不同的接口做同样的事情? I can't find anyplace where this is confirmed. 我找不到任何确认的地方。

My understanding is that the correct answer is No. 我的理解是,正确答案是否定的。

FileChannel .force calls fdatasync or fsync . FileChannel .force调用fdatasyncfsync This can be seen in jdk/src/solaris/native/sun/nio/ch/FileChannelImpl.c on of the OpenJDK source code. 这可以在OpenJDK源代码的jdk / src / solaris / native / sun / nio / ch / FileChannelImpl.c中看到。 FileDescriptor calls fsync (To find this out was more complex. I finally traced it to jvm.cpp). FileDescriptor调用fsync(要发现它更复杂。我最终将其追溯到jvm.cpp)。

I am the "author" of the linked question. 我是相关问题的“作者”。 So I was wrong. 所以我错了。 What is NOT enough is FileOutputStream.flush. FileOutputStream.flush是不够的 because it is a no-op. 因为它是无操作的。 You therefore either need force or sync. 因此,您需要强制或同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java FileChannel.force()和StandardOpenOption.Sync之间的区别是什么? - What is the diff between Java FileChannel.force() and StandardOpenOption.Sync? FileDescriptor.sync()线程安全 - FileDescriptor.sync() thread-safety 如果Filechannel.force(true)引发异常,则写入的数据将如何处理? - What happens with written data if Filechannel.force(true) throws an exception? RandomAccessFile.close()是否在内部调用FileChannel.force()? - Does RandomAccessFile.close() internally call FileChannel.force()? FileDescriptor.sync()是否适用于所有文件数据,还是仅适用于调用方JVM内的文件数据 - Does FileDescriptor.sync() work for all file data or just file data originating within the callers JVM FileChannel#强制和缓冲 - FileChannel#force and buffering java.io.FileDescriptor#sync()是否特定于单个FileDescriptor - Is java.io.FileDescriptor#sync() specific to a single FileDescriptor java.io.FileDescriptor.sync() 是否在 Linux 上同步目录? - Does java.io.FileDescriptor.sync() fsync directories on Linux? FileChannel#force是否等效于OutputStream#flush? 我总是必须打电话吗? - Is FileChannel#force is equivalent to OutputStream#flush? Do I always have to call it? 如何强制休眠以同步序列? - How force hibernate to sync sequences?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM