首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Unsafe.storeFence()文档错误吗?

Java Unsafe.storeFence()文档错误吗?
EN

Stack Overflow用户
提问于 2015-06-02 15:42:22
回答 2查看 1.2K关注 0票数 11

Java 8在sun.misc.Unsafe中增加了三个栅栏。

在我看了他们的文件后,我感到困惑。

所以,我搜索了网络,找到了这个链接

根据上面这一页,我认为这些方法在实践中几乎没有增加任何东西。如果我错了,粗略地说,loadFence()、storeFence()和fullFence()分别对应于易失读、延迟写入和易失性写入,尽管从技术上讲,这些栅栏比易失变量强。因此,loadFence()是获取围栏,storeFence()是释放围栏,fullFence()是完全隔离。

但是,storeFence()的文档看起来很奇怪。

上面写着,

代码语言:javascript
复制
/**
 * Ensures lack of reordering of stores before the fence
 * with loads or stores after the fence.
 */
void storeFence();

那看起来不像释放围栏。它应该怎么用?不是应该吗

代码语言:javascript
复制
/**
 * Ensures lack of reordering of loads or stores before the fence
 * with stores after the fence.
 */
void storeFence();

我假设之前意味着更早,之后意味着更晚。

编辑

我的意思不是“我们在通常的发展中不使用它们”,当我说“篱笆在实践中没有增加任何东西”时。

我的意思是,即使没有这些不安全的方法,我们也可以得到这些“栅栏”。如果我是正确的,在实践中,读取虚拟易失性具有loadFence()的效果,而编写虚拟易失性的效果是fullFence(),而unsafe.putOrderedXXX() (或AtomicInteger.lazySet())具有storeFence()的效果。

它们可能有细微的差别,但在目前的实现中,它们是可交换的。(似乎是链接暗示的)

这就是我所说的“他们没有增加任何新东西”。

另一个编辑

这个已经修好了。

请参阅https://bugs.openjdk.java.net/browse/JDK-8038978

谢谢@john

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-02 16:47:32

实际上,在JDK9中这是有区别的。提出并澄清了类似的问题:

http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/84e19392365e

代码语言:javascript
复制
      /**
!      * Ensures that loads before the fence will not be reordered with loads and
!      * stores after the fence; a "LoadLoad plus LoadStore barrier".
!      *
!      * Corresponds to C11 atomic_thread_fence(memory_order_acquire)
!      * (an "acquire fence").
!      *
!      * A pure LoadLoad fence is not provided, since the addition of LoadStore
!      * is almost always desired, and most current hardware instructions that
!      * provide a LoadLoad barrier also provide a LoadStore barrier for free.
       * @since 1.8
       */
      public native void loadFence();

      /**
!      * Ensures that loads and stores before the fence will not be reordered with
!      * stores after the fence; a "StoreStore plus LoadStore barrier".
!      *
!      * Corresponds to C11 atomic_thread_fence(memory_order_release)
!      * (a "release fence").
!      *
!      * A pure StoreStore fence is not provided, since the addition of LoadStore
!      * is almost always desired, and most current hardware instructions that
!      * provide a StoreStore barrier also provide a LoadStore barrier for free.
       * @since 1.8
       */
      public native void storeFence();

      /**
!      * Ensures that loads and stores before the fence will not be reordered
!      * with loads and stores after the fence.  Implies the effects of both
!      * loadFence() and storeFence(), and in addition, the effect of a StoreLoad
!      * barrier.
!      *
!      * Corresponds to C11 atomic_thread_fence(memory_order_seq_cst).
       * @since 1.8
       */
      public native void fullFence();
票数 6
EN

Stack Overflow用户

发布于 2015-06-02 15:52:49

我相信这些方法在实践中几乎没有增加任何东西。

没错,它们不会在99.9%的应用程序中添加任何内容。只有在非常特殊的情况下,您才需要直接调用此方法,而不是使用更高级别的构造。

易失读,延迟写入和易失性写入,

我把它读成“易失读,易失性写,易失性写和读”,似乎没有懒散/有序的写栏。

loadFence()是获取围栏,storeFence()是释放围栏,

我不相信这些转换为获取/发布语义。他们比这更基本。例如,这些方法中没有状态更改。获取/发布需要原子操作,例如compareAndSet,这是另一种不安全的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30600621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档