Questions tagged [concurrentmodification]

Concurrent modification is a common problem with some thread-using applications, when not properly using locks/syncronization. It may cause errors or exceptions, such as ConcurrentModificationException in Java.

Concurrent modification is an error that may come up at random times in threaded code. For example, consider two functions, to withdraw and add from an account:

public variable dollars

add(amount):
   add amount to dollars
   sets dollars amount


withdraw(amount):
   subtract amount from dollars
   sets dollars amount

If this pseudocode example had two threads running, it would be possible to add one to the number (100 to 101 dollars), concurrently dispensing $100 to customer and setting account to 0, then set the account to 101 dollars.

Different languages handle this differently, for example Java has synchronized(object with lock), GTK has gtk_threads_enter/leave for interacting on single thread.

622 questions
4
votes
1 answer

ConcurrentModificationException on isEmpty() method of ArrayList

I have the following code: Map> map; for(String k : map.keySet()){ List list = map.get(k); boolean empty = list.isEmpty();//CME if(!empty && somecheck(k, ...)){ list.clear(); } } And I'm getting…
TEXHIK
  • 1,369
  • 1
  • 11
  • 34
4
votes
4 answers

Performance difference between Iterator Class and foreach construct

I have the following code running, but I sometimes get some sort of concurrency exception when running it. ArrayList carriers = new ArrayList(); ArrayList mobs = new ArrayList(); ... for (Mob carrier : carriers){ for (Mob mob…
allan
  • 43
  • 1
  • 3
4
votes
2 answers

Remove elements from HashSet on iteration

Suppose I have a HashSet: [1, 2, 3, 4, 5, 6] I want to iterate over it in such a way, that for a given sum, say 6, while iterating over the elements, if I find 2 elements in the Set having sum = 6, I want to remove the other one. E. g., if I am…
clever_bassi
  • 2,392
  • 2
  • 24
  • 43
4
votes
2 answers

java.util.ConcurrentModificationException But I am not removing

My below recursive function throws a ConcurrentModificationException on the 'continue' statement. I looked at a few posts on ConcurrentModificationException and all of the problems seem to be with removing an element from an element, but I am not…
Rohan
  • 1,312
  • 3
  • 17
  • 43
4
votes
4 answers

Reason for ConcurrentModificationException on ArrayLists iterator.next()

I have no idea why a ConcurrentModificationException occurs when i iterate over an ArrayList. The ArrayList is methode scoped, so it should not be visible by other threads which execute the same code. At least if i understodd multi threading and…
Joysn
  • 987
  • 1
  • 16
  • 30
4
votes
2 answers

ConcurrentModificationException without modifying object

I've got the following piece of code that is causing a ConcurrentModificationException. I'm not modifying the products object at all, List products = Product.findActiveByFilter(filters); Set temp = new HashSet(); List
Diamondo25
  • 769
  • 1
  • 8
  • 21
4
votes
4 answers

ConcurrentModificationException when using iterator and iterator.remove()

private int checkLevel(String bigWord, Collection dict, MinMax minMax) { /*value initialised to losing*/ int value = 0; if (minMax == MinMax.MIN) value = 1; else value = -1; boolean go = true; …
dwjohnston
  • 11,163
  • 32
  • 99
  • 194
4
votes
4 answers

ConcurrentModificationException and HashSet.iterator()

I have a for loop like for (int neighbour : neighbours) { Where I may modify neighbours within the loop. Found that thats the cause of ConcurrentModificationException. And read from https://stackoverflow.com/a/8189527/292291 Hence if you…
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
3
votes
5 answers

java.util.ConcurrentModificationException On MapView

fellas I am facing very strange issue from many days. I am trying to update overlay frequently. So sometime I am getting "java.util.ConcurrentModificationException" when I touch on map or sometime getting when map trying to update overlay But I am…
3
votes
2 answers

Does Java have a data structure that supports Concurrent Modification?

I'm making a game in Java. Every enemy in the game is a thread and they are constantly looping through the game's data structures (I always use the class Vector). Lately I have been getting the "ConcurrentModificationException" because an element…
Rama
  • 4,697
  • 5
  • 22
  • 19
3
votes
3 answers

getting concurrentmodification error while looping on hashmap

I have the following error: Exception in thread "Thread-0" java.util.ConcurrentModificationException at java.base/java.util.HashMap$HashIterator.nextNode(HashMap.java:1584) at java.base/java.util.HashMap$KeyIterator.next(HashMap.java:1607) at…
duh ikal
  • 161
  • 1
  • 8
3
votes
4 answers

ConcurrentModificationException: Why does removing the null in List throw this Exception if it´s not the first variable

I got this code Snippet which is working fine. import java.util.ConcurrentModificationException; import java.util.*; ArrayList s = new ArrayList<>(); s.add(null); s.add("test"); try { System.out.println(s + "\n"); for (Object t :…
3
votes
1 answer

Java HashMap ConcurrentModification Exception despite using synchronized block

I have a hashmap used in multiple threads at the same time. To make it thread safe I put it into a synchronized block: private final Map mapConnections = new HashMap()<>; ... synchronized (mapConnections) { …
3
votes
1 answer

Move current object on iterator to end of list

I'm having a problem on Java using Iterator (LinkedList.iterator()) object. In a looping, I need move a iterator object from some place to end of list. For instance: final Iterator it = this.transitions.iterator(); while(it.hasNext()) { …
David Rodrigues
  • 12,041
  • 16
  • 62
  • 90
3
votes
5 answers

Is there an accepted best practice in Java for deleting a list element while iterating over the list?

I'm finding conflicting advice over the best way to avoid a ConcurrentModificationException while doing this: List Apples = appleCart.getApples(); for (Apple apple : Apples) { delete(apple); } I'm leaning towards…
Jonathan
  • 31
  • 2