像使用同步关键字时一样,在 CyclicBarrier 或 CountDownLatch 上进行缓存刷新

发布于 2024-12-05 18:26:59 字数 113 浏览 2 评论 0原文

有没有什么方法可以确保java刷新在CyclicBarrier或CountDownLatch允许我们继续(就像synchronized关键字一样)之前已经完成的写入的缓存,而不使用synchronized关键字?

Is there some way how to ensure that java flushes the cache of writes that have been done before the CyclicBarrier or CountDownLatch allows us to continue (as the synchronized keyword does) without using the synchronized keyword?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-12-12 18:26:59

我认为API已经保证了这一点。

http://download.oracle.com/javase /6/docs/api/java/util/concurrent/CyclicBarrier.html

内存一致性影响:调用await() 之前线程中的操作发生在操作中,这些操作是屏障作用,从其他线程中相应的await() 成功返回之后,这些操作又会发生在 之前。

http://download.oracle .com/javase/6/docs/api/java/util/concurrent/package-summary.html#MemoryVisibility

仅当满足以下条件时,才保证一个线程的写入结果对另一个线程的读取可见。写操作发生在读操作之前。 ... 调用 CyclicBarrier.await 屏障操作执行的 happen-before 操作之前的操作,以及成功返回之后屏障操作 happen-before 操作执行的操作来自其他线程中相应的等待。


这意味着

thread 1                  thread 2

write x1;                 write x2
barrier.await();          barrier.await();
read x2                   read x1

不需要额外的同步; read x2 将看到 write x2 的结果

I think it is already guaranteed by the API.

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/CyclicBarrier.html

Memory consistency effects: Actions in a thread prior to calling await() happen-before actions that are part of the barrier action, which in turn happen-before actions following a successful return from the corresponding await() in other threads.

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html#MemoryVisibility

The results of a write by one thread are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation. ... Actions prior to calling CyclicBarrier.await happen-before actions performed by the barrier action, and actions performed by the barrier action happen-before actions subsequent to a successful return from the corresponding await in other threads.


This means

thread 1                  thread 2

write x1;                 write x2
barrier.await();          barrier.await();
read x2                   read x1

no additional synchronization is needed; read x2 will see the result of write x2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文