This class provides a thread safe map interface (by extending Hashtable) and adds a way to easily and synchronously
iterator over the list of values as an array. This map is very useful when you want a snap shot of the values to
iterator over and dont want to hold up access to the map the whole time while you are iteratoring over the list to
avoid concurrent modification exception.
For example :
Hashtable t = ....
for(Iterator i = t.values().iterator(); i.hashNext(); ) {
// do something
}
In the above code, if multiple threads are accessing t, to avoid ConcurrentModificationException, you need to
synchronize the entire for loop.
Using CopyOnWriteArrayMap and using the values() method will give you a snapshot of the values thus avoid
synchronizing the map for the entire duration of the for loop.
This is achieved by maintaining an internal copy of the values in an array and copying that on modification. So an in
any CopyOnWrite class this is only effective on small datasets with lots of reads and few writes.
|