[英]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调用fdatasync或fsync 。 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.