Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I have read iteration doesn't hold lock,so what does above statement means? </p> </blockquote> <p>One can argue that the size method can go ahead and never hold a lock. But this implementation will get the size of the ConcurrentHashMap twice, if the size of first does not equal second it will retry. If the same is true it will lock all segments and get the size a last time. </p> <blockquote> <p>Do operations like get can also hold lock? Please provide scenario also.</p> </blockquote> <p>Technically yes it can but probably will never happen. In the event a JVM publishes one of the CHM's entry entry value after the entry is made available the CHM will do the read under segment lock (again this probably will never happen).</p> <p>Java 8 is releasing a new implementation of CHM so this is probably going to be outdated soon.</p> <blockquote> <p>Will the update operation running in thread 1 be visible to iteration in thread 2 if iteration over that element is yet to be made?(volatility?)</p> </blockquote> <p>If thread 1 issues the put prior to thread 2 issuing the get, then thread 2 will see the updated entry. </p> <p>If, while thread 2 is doing a get at the same time thread 1 is doing the put then thread 2 may or may not see the entry (or it may, it's a matter of timing). This is because <code>get</code> is non-blocking. This is still thread safe because the CHM says that it will return the entry as it is at the moment you are searching the map.</p> <blockquote> <p>Are there any other situations besides updation where locking is done?</p> </blockquote> <p>Besides all modification methods, serialization requires a lock.</p> <blockquote> <p>When getting data, a volatile read is used. If the volatile read results in a miss, then the lock for the segment is obtained for a last attempt at a successful read.What does this mean?What is volatile read?</p> </blockquote> <p>I have already eluded to this with my "technically" answer. What you are referring to is the <code>readValueUnderLock</code> method. According to the JMM the write of a non-final field, whether inline construction or within the constructor, inside an object can be published after the Object is available to another thread.</p> <p>So </p> <pre><code>public Entry{ volatile Object value; public Entry(Object v){ value = v; } } Thread 1 Entry e = new Entry(new Object()); Thread 2 if(e != null) Object value = e.value; // here, according to the JMM, value can be null. // If value were final it would never be null </code></pre> <p>Reading the value under the lock synchronizes the read with the previous write from the other thread so that will always prevent a null value.</p> <p>That all being said, this scenario is either highly unlikely or under x86 arch impossible.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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