• Post Reply Bookmark Topic Watch Topic
  • New Topic

Different behaviours between ConcurrentSkipListMap and CopyOnWriteArrayList  RSS feed

 
Edwardd Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Edwardd Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!