Cache flush on CyclicBarrier or CountDownLatch like when using synchronized keyword

倾然丶 夕夏残阳落幕 提交于 2019-12-11 03:54:52

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7493140/cache-flush-on-cyclicbarrier-or-countdownlatch-like-when-using-synchronized-keyw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!