gpt4 book ai didi

java - Preferences.flush() 和 Preferences.sync() 有什么区别?

转载 作者:行者123 更新时间:2023-11-30 02:02:10 46 4
gpt4 key购买 nike

Preferences.flush()Preferences.sync() 有什么区别?

来自Javadoc(flush()):

Forces any changes in the contents of this preference node and its descendants to the persistent store. Once this method returns successfully, it is safe to assume that all changes made in the subtree rooted at this node prior to the method invocation have become permanent.

Implementations are free to flush changes into the persistent store at any time. They do not need to wait for this method to be called.

When a flush occurs on a newly created node, it is made persistent, as are any ancestors (and descendants) that have yet to be made persistent. Note however that any preference value changes in ancestors are not guaranteed to be made persistent.

If this method is invoked on a node that has been removed with the removeNode() method, flushSpi() is invoked on this node, but not on others.

sync():

Ensures that future reads from this preference node and its descendants reflect any changes that were committed to the persistent store (from any VM) prior to the sync invocation. As a side-effect, forces any changes in the contents of this preference node and its descendants to the persistent store, as if the flush method had been invoked on this node.

看看java.util.prefs.FileSystemPreferencesjava.util.prefs.WindowsPreferences的实现,flush()仅仅调用sync(),就是这样。

处理其他 java.util.prefs 实现时(如果它们遵循规范):

  • 如果我只使用 flush() 而不是 sync(),我丢失未保存数据的可能性有多大(例如,在断电或 JVM 异常终止的情况下) >?
  • 此外,我的理解是否正确:sync() 保证更改对其他 JVM 可见,而 flush() 则不然?

最佳答案

flush()

刷新基本上执行对首选项节点的任何更改到后备存储的写入操作,包括所有子节点上的数据。

sync()

它确保内存中首选项节点的当前版本与存储的版本匹配,并且基本上 Sync() 会阻止执行,因此使用 flush() 方法来避免未保存的数据.

关于java - Preferences.flush() 和 Preferences.sync() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52495268/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com