0

I have a problem with this source code. The outer for loop doesn't iterate:

java.util.ConcurrentModificationException 
     at java.util.Hashtable$Enumerator.next(Hashtable.java:1387)

The Foo function only reads from myVar and returns a Hashtable .

As a matter of not rewriting myVar function, I've created an auxiliary variable called iteratingHashTable.

while (condition) {

    Hashtable<String, Boolean> iteratingHashTable = new Hashtable<String, Boolean>();

    iteratingHashTable = myVar;

    for (Map.Entry entry : iteratingHashTable.entrySet()) {

        if (value.equals(entry.getValue())) {
            String analyzingURL = entry.getKey().toString();
            myVar.replace(analyzingURL, true);

            Hashtable<String, Boolean> listedLinks = Foo(myVar, entry.getKey().toString());

            if (listedLinks != null) {

                for (Map.Entry entry1 : listedLinks.entrySet()) {

                    if (!entry1.getKey().toString().isEmpty()) {
                        myVar.put(entry1.getKey().toString(), entry1.getValue().equals(true));
                    }
                }
            }
        } else {
            condition = false;
        }
    }
}
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
  • Are you accessing myvar from multiple threads concurrently? On a sidenote: If you want to iterate in this manner maybe a HashMap is not the ideal datastructure to use? – Jonaswg Mar 13 '19 at 09:44
  • You modify the same object even if you are using different variables. – Andrianekena Moise Mar 13 '19 at 10:01

3 Answers3

1

I think but I'm not sure:

iteratingHashTable = myVar;

are referencing the same object, so when in the code below you try to use myVar and iteratingHashTable for different operations your are always using the same object.

Miao
  • 85
  • 9
1

If the Hashtable is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.

When you are using normal collection class (not the concurrent collection class), you are not allowed to do any modification on the list in the for-loop. Because of fail-fast behavior. That means, when iterating items of the collection if you do any modification of the same list or map, it'll throw concurrentModificationException.

To Resolve the above issue, use concurrentHashMap or create one more hashMap and add or replace these elements in that map and after completion of for loop add those elements to the original map.

Venkat
  • 11
  • 2
0
  • Java Collection classes are fail-fast, which means if the Collection will be changed while some thread is traversing over it, the iterator.next() will throw ConcurrentModificationException.

  • In your case, you are iterating the hashtable and changing the value of the same hence this exception thrown. as you assigning iteratingHashTable = myVar; both variable points to same.

  • You can avoid this exception in following ways :

    1) If you are using >= JDK1.5, you can use ConcurrentHashMap and CopyOnWriteArrayList (recommended approach)

    2) You can use synchronized block to lock the table while iterating.

kavita
  • 417
  • 4
  • 8