Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Therefore writing an element within that Object (if it is e.g. a standard HashMap, performing a put() operation on it) will not establish such a relationship. Is that correct?</p> </blockquote> <p>Yes and no. There is always a happens-before relationship when you read or write a <code>volatile</code> field. The issue in your case is that even though there is a happens-before when you access the <code>HashMap</code> field, there is no memory synchronization or mutex locking when you are actually operating on the <code>HashMap</code>. So multiple threads can see different versions of the same <code>HashMap</code> and can create a corrupted data structure depending on race conditions.</p> <blockquote> <p>Now, with using a volatile ConcurrentHashMap, will writing an element to it establish the happens-before relationship, i.e. will the above still work?</p> </blockquote> <p>Typically you do <em>not</em> need to mark a <code>ConcurrentHashMap</code> as being <code>volatile</code>. There are memory barriers that are crossed internal to the <code>ConcurrentHashMap</code> code itself. The only time I'd use this is if the <code>ConcurrentHashMap</code> field is being changed often -- i.e. is non-final. </p> <p>Your code really seems like premature optimization. Has a profiler shown you that it is a performance problem? I would suggest that you just synchronize on the map and me done with it. Having two <code>ConcurrentHashMap</code> to solve this problem seems like overkill to me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload