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

Custom Validation Annotation introduces ConcurrentModificationException

I was tasked with creating an Annotation for Custom Validation. This was due to some problems with handling database constraint violations nicely. What I did in response to this was relatively simple. I created a class-level CustomConstraint…
Vogel612
  • 5,620
  • 5
  • 48
  • 73
3
votes
3 answers

ConcurrentModificationException: .add() vs .addAll()

Why does the following occur? Shouldn't both work? List items = data; for( String id : items ) { List otherItems = otherData; // 1. addAll() //Causes ConcurrentModificationException …
FreakyDan
  • 559
  • 1
  • 7
  • 24
3
votes
3 answers

ConcurrentModificationException while trying to implement Dijkstra algorithm

I'm trying to implement Dijkstra's shortest path algorithm in my maze. ( http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm ) I got two HashSets, one for visited and one for unvisited fields. Once a field has been visited by all its neighbors…
CthenB
  • 800
  • 6
  • 17
3
votes
3 answers

EnumMap with concurrent put/get

I am considering using EnumMap in a concurrent environment. However, the environment is atypical, here's why: EnumMap is always full: there are no unmapped keys when the map is exposed to the concurrent environment Only put() and get() operations…
Tadas S
  • 1,955
  • 19
  • 33
3
votes
1 answer

ConcurrentModificationException TimerTask

I get ConcurrentModificationException when I start a second timer after canceling the first one. Both timers uses a separate ArrayList and iterates through it. No removal/modification is performed on the list, still there is thrown…
yonikawa
  • 581
  • 1
  • 9
  • 32
3
votes
5 answers

java.util.ConcurrentModificationException in For loop

I am trying to program an IM software, I want to let user leave the conversation and tell his partner that he has left... I prefer to use for loop instead Iterator, seek all the users and get the user who ask to leave and remove him... like that: …
EsmaeelQash
  • 488
  • 2
  • 6
  • 20
3
votes
2 answers

List ConcurrentModificationException

I have the following code public void saveProjects(List proj) throws DatabaseException { for (Project listItems: proj) { // error here insertProjects(listItems); } } private void insertProjects(Project prj) throws…
Jacob
  • 14,463
  • 65
  • 207
  • 320
3
votes
4 answers

java.util.ConcurrentModificationException upon LinkedHashSet iteration

Please, help me understand the error I am getting: private void replayHistory() { synchronized (alarmsHistory) { for (AlarmEvent alarmEvent : alarmsHistory) { LOG.error("replayHistory " + alarmEvent.type + " " +…
Łukasz
  • 1,980
  • 6
  • 32
  • 52
3
votes
2 answers

ConcurrentModificationException when printing the contents of a vector

Ok so I'm wanting to write a program which allows the user to enter an infinite amount of numbers and then they would be printed in the same order again. I am getting this error: Exception in thread "main" java.util.ConcurrentModificationException …
booky99
  • 1,436
  • 4
  • 27
  • 45
3
votes
3 answers

How to simulate ConcurrentModificationException in own class?

I have been reading "Effective Java" Item 60, which is "Favor the use of standard exceptions". Another general-purpose exception worth knowing about is ConcurrentModificationException. This exception should be thrown if an object that was designed…
3
votes
4 answers

Concurrent Modification Exception in Java

I am getting the ConcurrentModificationException while executing this code. I am unable to figure out why it is happening? private void verifyBookingIfAvailable(ArrayList list, int id) { Iterator iterator =…
muneikh
  • 2,067
  • 5
  • 25
  • 59
3
votes
2 answers

Prevent modification of custom class while iterating

If I have a class with the interface: class AnIteratable(object): def __init__(self): #initialize data structure def add(self, obj): # add object to data structure def __iter__(self): #return the iterator def next(self): …
elhefe
  • 3,404
  • 3
  • 31
  • 45
3
votes
4 answers

issue with hashmap getting concurrent modification exception

I am getting the below error while using map and performing some remove.How to avoid this ? Caused by: java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793) at…
pars
  • 153
  • 2
  • 4
  • 11
2
votes
1 answer

Groovy, collating list causes concurrentmodification exception

Still learning the ropes with Groovy, this problem has tripped me up since last night. Not sure why it's throwing concurrentmod exception...(Java 1.6, Groovy 1.8.4) I have a list of keys... [1,2,3,4,5,6,7,8,9,10,11,12,13] I collate the list using a…
raffian
  • 31,267
  • 26
  • 103
  • 174
2
votes
3 answers

concurrent modification exception java, I dont think I can use iterator here?

I have a list I need to iterate over and delete certain items. I can't use an iterator because I need to call methods for each item (such as ls.getStatus()) which doesn't work with an iterator. If ls.getStatus() == 0 I need to delete that item. How…
user1212520
  • 501
  • 1
  • 5
  • 15