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
181
votes
10 answers

Why am I not getting a java.util.ConcurrentModificationException in this example?

Note: I am aware of the Iterator#remove() method. In the following code sample, I don't understand why the List.remove in main method throws ConcurrentModificationException, but not in the remove method. public class RemoveListElementDemo { …
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
168
votes
8 answers

Why is a ConcurrentModificationException thrown and how to debug it

I am using a Collection (a HashMap used indirectly by the JPA, it so happens), but apparently randomly the code throws a ConcurrentModificationException. What is causing it and how do I fix this problem? By using some synchronization, perhaps? Here…
mainstringargs
  • 13,563
  • 35
  • 109
  • 174
69
votes
11 answers

Getting a ConcurrentModificationException thrown when removing an element from a java.util.List during list iteration?

@Test public void testListCur(){ List li=new ArrayList(); for(int i=0;i<10;i++){ li.add("str"+i); } for(String st:li){ if(st.equalsIgnoreCase("str3")) li.remove("str3"); } …
hguser
  • 35,079
  • 54
  • 159
  • 293
46
votes
9 answers

Concurrent Modification exception

I have this little piece of code and it gives me the concurrent modification exception. I cannot understand why I keep getting it, even though I do not see any concurrent modifications being carried out. import java.util.*; public class SomeClass…
Ankit
33
votes
4 answers

java.util.ConcurrentModificationException with iterator

I know if would be trying to remove from collection looping through it with the simple loop I will be getting this exception: java.util.ConcurrentModificationException. But I am using Iterator and it still generates me this exception. Any idea why…
user2219247
  • 1,313
  • 10
  • 20
  • 26
25
votes
4 answers

How to modify a Collection while iterating using for-each loop without ConcurrentModificationException?

If I modify a Collection while iterating over it using for-each loop, it gives ConcurrentModificationException. Is there any workaround?
aps
  • 2,452
  • 10
  • 35
  • 47
25
votes
2 answers

Behavior of entrySet().removeIf in ConcurrentHashMap

I would like to use ConcurrentHashMap to let one thread delete some items from the map periodically and other threads to put and get items from the map at the same time. I'm using map.entrySet().removeIf(lambda) in the removing thread. I'm…
23
votes
4 answers

java.util.ConcurrentModificationException in Android animation

There is something I miss with the notion of Synchronizing code in Android. Scenario There are always 3 items drawn on the screen. Each image is stored in a ArrayList (lstGraphics). For this purpose I use a SurfaceView. Once the user taps on a…
Alin
  • 14,809
  • 40
  • 129
  • 218
21
votes
6 answers

ConcurrentModificationException with LinkedHashMap

Not sure what is triggering a java.util.ConcurrentModificationException when I iterate over the LinkedHashMap structure in the code below. Using the Map.Entry approach works fine. Did not get a good explanation on what is triggering this from the…
Aditya Sakhuja
  • 333
  • 1
  • 2
  • 6
17
votes
4 answers

Why does List.addAll of a reversed subList of the list cause a ConcurrentModificationException

I've been trying to take a sub list of a list, reverse it, and place the reversed list back into the starting position. For example, say we have the list [1, 2, 3, 4, 5, 6], then reversing from index 2 to index 4 would give [1, 2, 5, 4, 3, 6]. I've…
JolonB
  • 415
  • 5
  • 25
17
votes
5 answers

Collection throws or doesn't throw ConcurrentModificationException based on the contents of the Collection

The following Java code throws a ConcurrentModificationException, as expected: public class Evil { public static void main(String[] args) { Collection c = new ArrayList(); c.add("lalala"); …
Ravi Kuldeep
  • 233
  • 1
  • 10
15
votes
4 answers

Java HashMap add new entry while iterating

In a HashMap map = new HashMap(); it = map.entrySet().iterator(); while (it.hasNext()) { entry = it.next(); it.remove(); //safely remove a entry entry.setValue("new value"); //safely update current value //how to put…
Bruce
  • 8,609
  • 8
  • 54
  • 83
14
votes
4 answers

Why is there a ConcurrentModificationException even when list is synchronized?

I have Android multi-threading application. There is some probability that two or more triggers might run the same part of code. I have a list of objects. I made it to be synchronized by Collections.synchronizedList private List
snaggs
  • 5,543
  • 17
  • 64
  • 127
14
votes
5 answers

Avoiding TreeMap ConcurrentModificationException?

I am calling function that returns TreeMap instance, and in the calling code I wanted to modify the TreeMap. However, I am getting a ConcurrentModificationException. Here is my code: public Map function1() { Map
Nagappa L M
  • 1,452
  • 4
  • 20
  • 33
13
votes
7 answers

Using iterator on a TreeSet

SITUATION: I have a TreeSet of custom Objects and I have also used a custom Comparator. I have created an iterator to use on this TreeSet. TreeSet ts=new TreeSet(); Iterator itr=ts.iterator(); while(itr.hasNext()){ …
aps
  • 2,452
  • 10
  • 35
  • 47
1
2 3
41 42