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
12
votes
2 answers

ConcurrentModificationException in unmodifiable collection

I have this code below, and I'm getting a ConcurrentModificationException by executing the following line: filterCardsToDevice(getCollection()); the code: private List filterCardsToDevice(Collection col) { final List
Pedro Estevão
  • 982
  • 1
  • 13
  • 41
12
votes
6 answers

Collection - Iterator.remove() vs Collection.remove()

As per Sun , "Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress." I have two questions : What…
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
11
votes
3 answers

How to update an Atomic based on a condition?

How to update an AtomicInteger if its current value is less than the given value? The idea is: AtomicInteger ai = new AtomicInteger(0); ... ai.update(threadInt); // this call happens concurrently ... // inside AtomicInteger atomic…
Stephan Rozinsky
  • 553
  • 2
  • 6
  • 21
10
votes
3 answers

Why does it.next() throw java.util.ConcurrentModificationException?

final Multimap terms = getTerms(bq); for (Term t : terms.keySet()) { Collection C = new HashSet(terms.get(t)); if (!C.isEmpty()) { for (Iterator it =…
simpatico
  • 10,709
  • 20
  • 81
  • 126
10
votes
1 answer

What is a good way to implement reloading of a Typesafe config

In a Scala application that is using Typesafe Config, I want to add the possibility to reload a Config at runtime. A Config instance is immutable. Here is what I have so far: package config trait Settings { private[config] var config: Config =…
reikje
  • 2,850
  • 2
  • 24
  • 44
10
votes
5 answers

Why does one loop throw a ConcurrentModificationException, while the other doesn't?

I've run into this while writing a Traveling Salesman program. For an inner loop, I tried a for(Point x:ArrayList) { // modify the iterator } but when adding another point to that list resulted in a ConcurrentModicationException being…
Jason
  • 11,263
  • 21
  • 87
  • 181
10
votes
5 answers

Guava MultiMap and ConcurrentModificationException

I don't understand why I get a ConcurrentModificationException when I iterate through this multimap. I read the following entry, but I am not sure if I understood the whole thing. I tried to add a synchronized block. But my doubt is what to…
Antoine Claval
  • 4,923
  • 7
  • 40
  • 68
10
votes
4 answers

Why isn't this code causing a ConcurrentModificationException?

I was reading about ConcurrentModificationException and how to avoid it. Found an article. The first listing in that article had code similar to the following, which would apparently cause the exception: List myList = new…
user1699872
9
votes
6 answers

How to do concurrent modification testing for grails application

I'd like to run tests that simulate users modifying certain data at the same time for a grails application. Are there any plug-ins / tools / mechanisms I can use to do this efficiently? They don't have to be grails specific. It should be possible to…
9
votes
2 answers

How to avoid HashMap "ConcurrentModificationException" while manipulating `values()` and `put()` in concurrent threads?

Code: I have a HashMap private Map map = new HashMap<>(); One method will put K-V pair into it by calling put(K,V). The other method wants to extract a set of random elements from its values: int size = map.size(); // size > 0 V[]…
hengxin
  • 1,867
  • 2
  • 21
  • 42
9
votes
2 answers

LinkedList checkForComodification error java

Ok so what I am trying to do here is to have a method "running" a process for a given amount of "time" this all seams to work to a degree but it keeps giveing these eceptions. This is the first execption it gives Exception in thread "main"…
MNM
  • 2,673
  • 6
  • 38
  • 73
8
votes
4 answers

java.util.ConcurrentModificationException Streams

I was trying the following code Java 8 SE I ran it directly from eclipse, it has the below-mentioned exception also I ran it with command prompt it produces the same result. List test = new…
Joydeep
  • 401
  • 2
  • 6
  • 15
8
votes
2 answers

Collections.sort method sometimes throws ConcurrentModificationException in multithreaded environment . List is not being modified structurally

package CollectionsTS; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; public class ArrayListTS { public static void main(String[] args) { …
8
votes
3 answers

Abnormal behaviour of java.util.List based on number of elements in it

I know that if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw a ConcurrentModificationException. But it shows different behavior depending on the number of elements in the…
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
7
votes
2 answers

Stuck with "java.util.ConcurrentModificationException"

Here is my code: // eventList is a LinkedList public void run() { Iterator it = eventList.iterator(); int size = eventList.size(); while(size > 0) { while(it.hasNext()) { Event e = it.next(); //flaged line …
artaxerxe
  • 6,281
  • 21
  • 68
  • 106
1
2
3
41 42