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

MySQL atomic insert-if-not-exists with stable autoincrement

In MySQL, I am using an InnoDB table that contains unique names, and IDs for those names. Clients need to atomically check for an existing name, insert a new one if it does not exist, and get the ID. The ID is an AUTO_INCREMENT value, and it must…
user553702
  • 2,819
  • 5
  • 23
  • 27
2
votes
2 answers

UnsupportedOperationException vs ConcurrentModificationExcetion

I have a code that adds data to a list. What I do not understand is why the UnsupportedOperationException is thrown in one case and ConcurrentModificationException in the other. I am adding data in list in both the case and…
2
votes
3 answers

Java Properties variabile: set property while iterating keys

I have a code which collects all the system-properties keys which starts with a given prefix private static List getKeysByPrefix(String prefix) { Set keySet = System.getProperties().keySet(); Iterator iterator =…
2
votes
2 answers

How to solve ConcurrentModificationException when using a pair of lists in a recursive function?

So I am trying to balance a binary search tree by recursively setting the children of each node: public void balanceTheTree(){ root = balance(keys, names); } public Node balance(List keyList, List nameList){ …
2
votes
1 answer

Java ConcurrentModificationException at list element access

Can someone please explain to me why do I get a ConcurrentModificationException if I just want to access an element of the list? I was under impression that ConcurrentModificationException is thrown only when one thread iterates over a collection…
Viorel Morari
  • 537
  • 3
  • 10
2
votes
2 answers

Another ConcurrentModificationException question

I've searched StackOverflow and there are many ConcurrentModificationException questions. After reading them, I'm still confused. I'm getting a lot of these exceptions. I'm using a "Registry" setup to keep track of Objects: public class Registry { …
2
votes
2 answers

Maven Surefire Concurrent Modification Exception

When Maven builds my project and runs the unit tests, sometimes a concurrent modification exception is thrown (approximately 1 out of 5 times it will fail, the other times it will build successfully). But when I run the tests locally as unit tests,…
diepjy
  • 283
  • 2
  • 10
2
votes
2 answers

Getting ConcurrentModificationException from onLocationMethod

I am trying to update ArrayList of Locations as per below logic in a service, running in background thread through Runnable.I have edited code to show only relevant code. I am getting ConcurrentModificationException. public static…
2
votes
1 answer

ConcurrentModificationException when attempting to get an element from a List

So I am trying to create a simple Card game and to do this I have created a Player class that holds a name and the Player's hand. public class Player { private List hand; private String name; public Player(String name){ …
Rumple
  • 198
  • 1
  • 9
2
votes
1 answer

Why does removing a non-existent element from a map during iteration only crash sometimes?

Note: this is not a duplicate of the many questions asking how to remove an item from a map during iteration. I encountered some surprising edge cases when using a hash map iterator to remove an item from a map. The following code crashes with with…
AA A
  • 408
  • 7
  • 11
2
votes
2 answers

java - Remove nearly duplicates from a List

I have a List of Tweet objects (homegrown class) and I want to remove NEARLY duplicates based on their text, using the Levenshtein distance. I have already removed the identical duplicates by hashing the tweets' texts but now I want to remove texts…
2
votes
1 answer

java iterate list and addFirst at the same time

When I try to do ListIterator iter = list.listIterator(list.size()); for (int i = 0; i < size; i++) { iter.hasPrevious(); list.addFirst(iter.previous()); } I get ConcurrentModificationException. I know what that means, but how…
2
votes
3 answers

Android: Hashmap concurrent Modification Exception

I keep getting a concurrent modification exception on my code. I'm simply iterating through a hashmap and modifying values. From researching this I found people said to use iterators and iterator.remove, etc. I tried implementing with this and still…
2
votes
1 answer

Can IIS configuration be modified concurrently

We have multiple websites in IIS on each of our servers, and we have PowerShell scripts that can create/update an individual website, along with configuring the bindings, SSL, app pools etc. Given the size of the server farm, and the number of…
sheikhjabootie
  • 7,308
  • 2
  • 35
  • 41
2
votes
3 answers

Adding and removing elements to a Collection

I'm new to Java and I'm currently making this game where the player has to eat some cookies. These cookies are elements of an ArrayList. This ArrayList is modified by two threads : -one that iterates over it and removes the cookies that have been…
Quentin
  • 21
  • 3