0


I'm looking to avoid a ConcurrentModificationException where the functionality is to iterate over an expanding set (there are no removes), and the add operations are being done by different threads.

I considered cloning the collection before iterating, but if this solution doesn't scale very well as the set becomes large. Synchronizing doesn't work because the collection is being used in tonnes of places and the code is pretty old. Short of a massive refactoring, the only bet is to change the set implementation.

Wondering if there's a Java implementation where the iterator returns a snapshot state of the collection (which is okay for my functionality) but avoid the cost of cloning too much. I checked out CopyOnWriteArrayList but it doesn't fit the bill mainly because of being a list.

VGDIV
  • 679
  • 1
  • 7
  • 16
  • I'm assuming your collection isn't a set or you would have used [CopyOnWriteArraySet](http://download.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArraySet.html), so what kind of a collection is it? – Asaf Jun 17 '11 at 03:55
  • Hi Asaf as per the documentation- 'CopyOnWriteArraySet' : It is best suited for applications in which set sizes generally stay small. That doesn't scale very well. – VGDIV Jun 17 '11 at 04:40

2 Answers2

1

The java.util.concurrent package has everything you need.

The classes there are like the java.util collections, but are highly optimized to cater for concurrent access, interestingly addressing specifically your comment:

the iterator returns a snapshot state of the collection

Don't reinvent the wheel :)

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • The steps are : 1. I create the iterator 2. Some other thread modifies the collection 3. I get a ConcurrentModification Exception. Or am I missing something? – VGDIV Jun 17 '11 at 04:32
  • Oops Turns out I may infact be reinventing the wheel. Both @Bohemian and @Vineet are right. I can use the ConcurrentHashMap as a Set for this situation. My oversight! – VGDIV Jun 17 '11 at 04:47
0

Wondering if there's a Java implementation where the iterator returns a snapshot state of the collection

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.

Depending on the exact nature of your problem, ConcurrentHashMaps can be used. The relevant section of the documentation of the class, that applies to your problem is:

Iterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException. However, iterators are designed to be used by only one thread at a time.

Note the last sentence carefully.

Also, remember that these are not consistent snapshots of the collection being returned. The iterators from most methods returned possess the following property:

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.

Related questions

Community
  • 1
  • 1
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • Thanks, just realized you're right - not sure why I was using synchronizedSet fromt he beginning. – VGDIV Jun 17 '11 at 04:48