像使用同步关键字时一样,在 CyclicBarrier 或 CountDownLatch 上进行缓存刷新
有没有什么方法可以确保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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为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 操作执行的操作来自其他线程中相应的等待。
这意味着
不需要额外的同步;
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
no additional synchronization is needed;
read x2
will see the result ofwrite x2