是否需要FileChannel.force和FileDescriptor.sync? [英] Are FileChannel.force and FileDescriptor.sync both needed?

查看:259
本文介绍了是否需要FileChannel.force和FileDescriptor.sync?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://stackoverflow.com/questions/730521/really-作者在写的答案摘要中写道:

In https://stackoverflow.com/questions/730521/really-force-file-sync-flush-in-java, the author writes in the summary of the answers:


使用c .force(true)后跟s.getFD()。用于Java NIO的sync()

Use c.force(true) followed by s.getFD().sync() for Java NIO

我的问题是:你真的需要吗?都?力不够吗?是不是强制和同步只是不同的接口做同样的事情?我无法找到确认这一点的任何地方。

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调用 fdatasync fsync 。这可以在OpenJDK源代码的jdk / src / solaris / native / sun / nio / ch / FileChannelImpl.c中看到。
FileDescriptor 调用fsync (为了找到这个更复杂。我最终将其追溯到jvm.cpp)。

FileChannel.force calls fdatasync or fsync. This can be seen in jdk/src/solaris/native/sun/nio/ch/FileChannelImpl.c on of the OpenJDK source code. FileDescriptor calls fsync (To find this out was more complex. I finally traced it to jvm.cpp).

我是链接问题的作者。所以我错了。什么是不够的是 FileOutputStream.flush。因为它是一个无操作。因此,您需要强制或同步。

I am the "author" of the linked question. So I was wrong. What is NOT enough is FileOutputStream.flush. because it is a no-op. You therefore either need force or sync.

这篇关于是否需要FileChannel.force和FileDescriptor.sync?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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