Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by Alexey Malev for Avoiding concurrent structures by manually triggering memory barriers

Some value is written to volatile variable happens-beforethis value can be read from it. As a consequence, the visibility guarantees you want will be achieved by reading/writing it, so the answer is yes, this solves visibility issues.

Besides the problems mentioned by Darren Gilroy in his answer, I'd like to remember that in Java 8 there are explicit memory barrier instructions in Unsafe class:

/** * Ensures lack of reordering of loads before the fence * with loads or stores after the fence. */void loadFence();/** * Ensures lack of reordering of stores before the fence * with loads or stores after the fence. */void storeFence();/** * Ensures lack of reordering of loads or stores before the fence * with loads or stores after the fence. */void fullFence();

Although Unsafe is not a public API, I still recommend to at least consider using it, if you're using Java 8.

One more solution is coming to my mind. You have set your concurrencyLevel to 1 which means that only one thread at a time can do anything with a collection. IMO standard Java synchronized or ReentrantLock (for the cases of high contention) will also fit for your task and do provide visibility guarantees. Although, if you want one writer, many readers access pattern, consider using ReentrantReadWriteLock.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>