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
7
votes
1 answer

Java 8 ConcurrentModificationException when doing any kind of iteration

Trying to figure out this problem for 2 weeks, but without any success. :X It's occurring when I'm doing any kind of iteration, but mostly when using #forEach. I'm not modifying the list, nor it's elements, in any way, so this seems very awkward to…
delta
  • 185
  • 2
  • 13
7
votes
2 answers

How to keep two iterators over map in java and remove keys in between without ConcurrentModificationException

I have to process a Map > MyMap if (key1 contains all of corresponding true bits of key2) Remove from key2 all those values which are common with key1) In this process, if the number of elements in a list drops below…
Kaur
  • 279
  • 1
  • 6
  • 18
7
votes
7 answers

How can I fix this error java.util.ConcurrentModificationException

I get an error on the following line. I'm doing the process of adding to the jsonarray. Please help me. jsonArr=new JSONArray(); if(req.getSession().getAttribute("userses")!=null){ String…
aliplane
  • 875
  • 3
  • 8
  • 9
7
votes
6 answers

Java: Iterate over a set while contents of set are being modified

I wish to iterate over a set but the contents of the set will modify during its iteration. I wish to iterate over the original set at the time the iterator was created and not iterate over any of the new elements added to the set. How is this…
nomel7
  • 1,523
  • 3
  • 12
  • 20
6
votes
3 answers

In Java how can this throw a ConcurrentModificationException in a single threaded program?

I was reading this "Freuqent Java concurrency problems" question and got confused by an answer talking about java.util.ConcurrentModificationException. My understanding of the answer is that this can occur in a single-threaded program. How or what…
ArturPhilibin
  • 922
  • 2
  • 12
  • 25
6
votes
1 answer

I can't find the cause of my java.util.ConcurrentModificationException

I have code that enters the for loop within my Main method. for (List points : output) { currentPath = pathDistance(points); if (shortest == 0){ shortest = currentPath; } else if (currentPath < shortest) { best =…
6
votes
1 answer

How to make a list thread-safe for serialization?

I am using a ThreadSafeList and I am getting great mileage out of it for streaming data from a process to a webserver and then streaming the data back out as it comes in to a client. In memory I am saving the data in the JVM with Spring Caching…
5
votes
3 answers

Java List and recursion leads to Concurrent Modification Exception

The following function walks recursively through a list and divide it always by half and do something with the sublists. The recursion breaks when the listsize is 2. I know a concurrent modification exception occurs if I change the list when I…
Anthea
  • 3,741
  • 5
  • 40
  • 64
5
votes
2 answers

Avoiding ConcurrentModificationException on List by making a shallow copy

I have a class like the following: class Test { private LinkedList persons = new LinkedList; public synchronized void remove(Person person) { persons.remove(person); } public List
His
  • 5,891
  • 15
  • 61
  • 82
5
votes
1 answer

Modifying a SnapshotStateList throws ConcurrentModificationException

The documentation of SnapshotStateList states that it is similar to a regular mutable list. I have a use case where I need to modify all the elements in the list (set case). This does not change the size of the list, but I'm running into…
Naveed
  • 2,942
  • 2
  • 25
  • 58
5
votes
3 answers

Avoid ConcurrentModificationException using Iterator.next()

In my Android app I use this code while drawing some waypoints on a map Iterator iterator = waypoints.iterator(); while (iterator.hasNext()) { Waypoint w = iterator.next(); } But I am getting this error Fatal Exception:…
juergen d
  • 201,996
  • 37
  • 293
  • 362
5
votes
1 answer

why concurrent modification on foreach method but not on for loop

ArrayList targets = new ArrayList(); targets.add(2); targets.add(2); for (Integer testInt : targets ) { targets.add(1); } I am getting an concurrentModificationException,But with normal for loop. I am not getting any…
shiv
  • 477
  • 5
  • 17
5
votes
4 answers

Are private variables thread safe

Does a developer who is developing an api (like Collections api in java) should manually throw ConcurrentModificationException when two threads try to modify the object's data? Why does this piece of code not throw an exception as multiple threads…
Vicky
  • 3,230
  • 3
  • 18
  • 21
5
votes
6 answers

How can I iterate over an object while modifying it in Java?

Possible Duplicates: Java: Efficient Equivalent to Removing while Iterating a Collection Removing items from a collection in java while iterating over it I'm trying to loop through HashMap: Map group0 = new HashMap
Hristo
  • 45,559
  • 65
  • 163
  • 230
5
votes
2 answers

Does Collections.synchronized map makes Iterator threadsafe

There are two threads in a system. One is a reader thread and another is a writer thread. The map is synchronized using the following code. Map> m = Collections.synchronizedMap(new HashMap()) The…
Touchstone
  • 5,575
  • 7
  • 41
  • 48
1 2
3
41 42