Vivek Kr Singh wrote:ConcurrentHashMap size is not 25000 as expected. The value is lesser than 25000 and it is different on each run, which does suggest that size() is invoked on map when it is under modification.
Not sure what you mean by "size() is invoked on map when it is under modification" -- can you elaborate a bit?
Vivek Kr Singh wrote:
Any help would be appreciated.
I believe the issue (at least the one that I see) is here...
The index variable may be volatile, but that doesn't mean that all operations are thread
safe. It just means that atomic operations are thread safe -- as the JVM will not cache the values.
An increment is *not* an atomic operation in Java
. It is actually a load, increment in register, and store back to memory. Because of this, you may do a few parallel increments, and since a map don't allow duplicates, lose a few items.