Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Wondering if there's a Java implementation where the iterator returns a snapshot state of the collection</p> </blockquote> <p>Yes, there is. Unlike the synchronized collections made available via the Collections.synchronizedxxx() methods, the Concurrentxxx classes in java.util.concurrent package would allow for this scenario. The concurrent collection classes allow for multiple threads to access the collection at the same point in time, without the need to synchronize on a lock.</p> <p>Depending on the exact nature of your problem, <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ConcurrentHashMap.html" rel="nofollow noreferrer"><code>ConcurrentHashMaps</code></a> can be used. The relevant section of the documentation of the class, that applies to your problem is:</p> <blockquote> <p>Iterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. <strong><em>They do not throw ConcurrentModificationException</em></strong>. However, iterators are designed to be used by only one thread at a time.</p> </blockquote> <p>Note the last sentence carefully.</p> <p>Also, remember that these are not consistent snapshots of the collection being returned. The iterators from most methods returned possess the following property:</p> <blockquote> <p>The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.</p> </blockquote> <p><strong>Related questions</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/3768554/is-iterating-concurrenthashmap-values-thread-safe">Is iterating ConcurrentHashMap values thread safe?</a></li> <li><a href="https://stackoverflow.com/questions/3722991/java-concurrenthashmap-not-thread-safe-wth">Java ConcurrentHashMap not thread safe.. wth?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload