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

Solving a ConcurrentModificationException

I am writing a little game which has many circles moving on the screen. I am managing the circles in two threads as following: public void run() { int stepCount = 0; int dx; int dy; while (m_threadTrap){ dx = 0; dy =…
Nadavrbn
  • 25
  • 4
2
votes
6 answers

I keep getting java.util.concurrentmodificationexception.. How to fix this?

I've been working on this snippet of code. Here is the pseudocode of what I want to happen: a.check if sections(which is a list) size is 0. b.if sections size is 0, then automatically enroll the student to the section by calling…
황현정
  • 3,451
  • 6
  • 28
  • 35
2
votes
3 answers

How to clear all the elements in the String array in java?

I am getting java.util.ConcurrentModificationException since I am trying to load the elements dynamically, so I would like to clear all the elements in it. Kindly guide me on this how to clear on the elements or how can I do in any other way with a…
Karthik
  • 4,943
  • 19
  • 53
  • 86
2
votes
0 answers

python change a files creation and modification time without having to use a subcommand?

I want to change the modification and creation time of any file with python. This would be the command I use in Linux: touch -a -m -t 201512180130.09 ./file.jpg Then I check the following I can see the date is changed correctly: $ls -l…
Dave
  • 727
  • 1
  • 9
  • 20
2
votes
4 answers

ConcurrentModificationException, clarification needed

The following mockup code ends up in ConcurrentModificationException, that happens (as i understand it), due to the fact that i am iterating over a set, which i am modifying. Set data = new…
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
2
votes
1 answer

How can joinToString on a cloned ArrayList throw ConcurrentModificationException

I have an ArrayList of Strings, which is constantly changed (adding and removing lines) by a non-UI thread. In the UI I want to show the current content of that ArrayList in a simple TextView. When preparing the text for the TextView by…
user2808624
  • 2,502
  • 14
  • 28
2
votes
1 answer

Java: concurrent modification exception when iterating with a foreach loop

I know that when you iterate over an arraylist with a forech loop, and then trying to remove an object from it, the concurrent modification exception is thrown. I have the following code, which produces this exception whenever I try to remove any of…
Jessi
  • 69
  • 7
2
votes
1 answer

ConcurrentModificationException when Hibernate Upgrade from 4.x to 5.x and Spring 4.x for Multi-Threaded

I am having an issue when we migrated from Hibernate 4 to 5. I have been struggling from a week and read numerous blogs and pages, but could not resolve. Following are more information on the issue:- 1) Hibernate Version : From: 4.3.11.Final To :…
2
votes
0 answers

ConcurrentModificationException in Apache Velocity 1.7 with Java 8 and Ant 1.9

We have upgraded our project from java 7 and ant 1.8 to java 8 and ant 1.9. In java 7, we were able to use apache velocity 1.7 without any issues. However after upgrading to java 8, we get ConcurrentModificationException. Caused by:…
KRR
  • 43
  • 5
2
votes
2 answers

Can a single core processor still throw ConcurrentModificationException?

If I spawn 2 threads on a single core PC does it ever access for example an ArrayList in the same time so it will throw ConcurrentModificationException? My gut tells me although there are 2 threads, they cannot achieve true parallelism because there…
ZeNstok
  • 130
  • 1
  • 7
2
votes
1 answer

java.util.ConcurrentModificationException while mutating an object

I am iterating over a List of CustomObject and while doing this iteration, I am mutating this object by adding a tag to tags list of this custom object. I am not adding or removing any CustomObject to or from customObjects (List). I am still getting…
Mr Matrix
  • 1,128
  • 2
  • 14
  • 28
2
votes
2 answers

ConcurrentModificationException with ConcurrentHashMap

I understand the concept behind it but thought using ConcurrentHashMap instead of HashMap will fix it. Because ConcurrentHashMap protects from concurrent reading and modification by different threads. But I still see the exception. Here's the code…
R11G
  • 1,941
  • 8
  • 26
  • 37
2
votes
1 answer

Still getting concurrent modification exception with synchronizedList

Grettiings, I'd asked this before, but even after the changes I'm still getting Concurrent Modification Exception, I'd implemented synchronizedList and synchronized(list) but the exception is still happening, I running out of ideas, could you give…
2
votes
0 answers

ConcurrentModificationException while calling a method for every Object in a for each loop

Im currently building a shopping cart and I want to update the item stock in my database for each item in my cart after checkout. The items are stored in Session Scope as list of Java Beans. This is my checkout servlet. protected void…
DD-DD
  • 21
  • 1
2
votes
2 answers

How to Thread.join() on all elements of a list of changing size?

Lets say I have a large number of worker threads all actively processing, and a supervisor thread that waits for them all to complete. Traditionally I could do something like: for(Worker w:staff){ w.start(); } for(Worker w:staff){ …
Andrew
  • 617
  • 2
  • 6
  • 19