Forums Register Login

Different behaviours between ConcurrentSkipListMap and CopyOnWriteArrayList

+Pie Number of slices to send: Send
When I run this code:The output is , which is expected. However when I run a similar code using ConcurrentSkipListMap:
The output is: which is confusing for me. I always thought that colors.keySet() in the for braces in ConcurrentSkipListMap, like colors in CopyOnWriteArrayList, was established prior to a modification and will not be updated when changes are made. In which case the output of the second code should have been: If it is the case that colors.keySet() is updated as soon as a modification is made, the second code should have an infinite amount of outputs, since colors.keySet() never ends being added elements into. Why then, does the actual output has a finite result?
1
+Pie Number of slices to send: Send

It is all explained in the JavaDoc... The Iterator for the sets of the ConcurrentSkipListMap are Weakly Consistent, which are further explained as...

* they may proceed concurrently with other operations
* they will never throw ConcurrentModificationException
* they are guaranteed to traverse elements as they existed upon construction exactly once, and may (but are not guaranteed to) reflect any modifications subsequent to construction.



Henry
+Pie Number of slices to send: Send
Thanks Henry!
These are the worst of times and these are the best of times. And this is the best tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 341 times.
Similar Threads
error in for each
Enum Question
Struts 2 checkboxlist error - list key could not be resolved
permutations of the list
ConcurrentMap
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:56:29.