"...useful only when a map is not undergoing concurrent updates in other
threads..."
The size() method sums the segment field "modCount" twice and compares the
result to ensure no concurrent updates in other threads.
If there's concurrent updates, the size() method resort to locking
segments. So the size() method tries to get the mostly updated
result even if after the result returns the result may already be stale. In
java1.6 field "count" has volatile present and "modCount"
field read has happen-before relation with "count" write, so the sum of
"count" can has mostly updated result. But that's not the case for java 1.7.
In java1.7 the no volatile present field "modCount" and "count" may fail to
get mostly updated value.
On Monday, September 18, 2017 at 1:46:34 PM UTC+8, Nikolay Tsankov wrote:
>
> * Bear in mind that the results of aggregate status methods including
> * {@code size}, {@code isEmpty}, and {@code containsValue} are typically
> * useful only when a map is not undergoing concurrent updates in other
> threads.
> * Otherwise the results of these methods reflect transient states
> * that may be adequate for monitoring or estimation purposes, but not
> * for program control.
>
>
> On Mon, Sep 18, 2017 at 5:18 AM, yang liu <[email protected]
> <javascript:>> wrote:
>
>> Recently I studied the source code of ConcurrentHashMap.
>>
>> In java 1.7, the segment field "count" got no volatile modifiers which is
>> different from java 1.6.
>>
>> Is possible the size() method could read stale value through race read?
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "mechanical-sympathy" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.