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

Why do original variables change when new variables are changed?

I have the following block of code: ArrayList list1 = new ArrayList(); ArrayList list2 = list1; // both list1 and list2 are empty arraylists System.out.println(list1.size()); // prints:…
vcapra1
  • 1,988
  • 3
  • 26
  • 45
0
votes
8 answers

Java - Exception in thread "main" java.util.ConcurrentModificationException

Is there any way I can modify the HashMap values of a particular key while iterating over it? A sample program is given below: public static void main(String[] args) { HashMap> hm = new HashMap
Raghu
  • 29
  • 2
  • 2
  • 7
0
votes
3 answers

Remove two objects at the same time from an ArrayList without causing a ConcurrentModificationException?

I have the following code that is throwing a ConcurrentModificationException. Can someone please explain why this is happening? public void foo(ArrayList bets) Iterator it1 = bets.iterator(); while(it1.hasNext()) Bet b1 = (Bet)…
fulhamHead
  • 687
  • 1
  • 10
  • 23
0
votes
2 answers

ConcurrentModificationException, but no modification

I am currently programming a multiplayer game in Java. My current code (that is getting the error) is as so. @Override public void onClose(WebSocket conn, int code, String reason, boolean remote){ System.out.println("Socket disconnected."); …
anonmous
  • 137
  • 2
  • 9
0
votes
0 answers

ConcurrentModificationException when splitting String

I've been running into a ConcurrentModificationException when running the below code (which is a snippet of the actual loop) I tried following the answer to this question Concurrent Modification exception but found that the reall issue occurs when…
Sammy Guergachi
  • 1,986
  • 4
  • 26
  • 52
0
votes
1 answer

Is this solution to ConcurrentModificationException safe?

I have a 2nd Thread that i use to send messages using OSC. From the main Thread i add messages, I had a problem with ConcurrentModificationException. What I did to fix it is I made a new List with messages to add. In the 2nd Thread i add those…
clankill3r
  • 9,146
  • 20
  • 70
  • 126
0
votes
2 answers

ConcurrentModificationException in a runnable

I am developing a timer manager that will allow multiple countdown timers and I cant seem to figure out how to avoid this ConcurrentModificationException. I have look at other peoples responses to similar problems but still cant figure it out.…
DDukesterman
  • 1,391
  • 5
  • 24
  • 42
0
votes
1 answer

How to use GORM to automically update a group of objects

I am using GORM standalone (groovyVersion = '2.0.8', grailsVersion = '2.2.4', gormVersion = '1.3.7', h2Version = '1.3.170') and have a database of objects which will be undergoing frequent concurrent modification. These objects are…
Peter N. Steinmetz
  • 1,252
  • 1
  • 15
  • 23
0
votes
2 answers

Removing Item from Collection / Changing field of Object

public void searchOwner(List appts, String owner) { Appointments theOne = null; for (Appointments temp : appts) { if (owner.equalsIgnoreCase(temp.owner.name)) { System.out.println(temp.data); …
x Nuclear 213
  • 97
  • 1
  • 2
  • 9
0
votes
3 answers

how to interrupt putAll operation in middle to generate ConcurrentModificationException

I want to reproduce one scenario in which there are two threads accessing a shared HashMap. While one thread is copying the contents of the shared map into localMap using putAll() operation, second thread changes the shared map and…
Infotechie
  • 1,653
  • 6
  • 23
  • 35
0
votes
1 answer

Throws java.util.ConcurrentModificationException

So this is a function from my snake game code. Basically I was initially doing a for to go though the LinkedList that is the snake but since it was throwing the exception I thought changing it using iterators would help. Apparently not. How…
Luís Gonçalves
  • 338
  • 5
  • 17
0
votes
4 answers

Java How to add to an array list while looping

package biz.boulter.state; import java.awt.Color; import java.awt.Graphics2D; import java.util.ArrayList; import biz.boulter.sword.Game; import biz.boulter.sword.Particle; public class Menu implements State{ private ArrayList particles…
nedb
  • 586
  • 1
  • 4
  • 12
0
votes
2 answers

ConcurrentModificationException even though changing values outside of foreach

I have the following code boolean postojaoJePrijelaz = true; epsilonStanja = sljedecaStanja; while(postojaoJePrijelaz) { for (String epsilonStanje : epsilonStanja) { …
Mislav Javor
  • 1,429
  • 1
  • 15
  • 23
0
votes
0 answers

Concurrent Modification Exception in publishResults

I'm implementing Filter in my Custom Adapter but I got CME when I press the search icon in action bar. I do some research and edit my code but no effect. My code: private class TaskFilter extends Filter { @Override protected…
Minh Phan
  • 47
  • 6
0
votes
3 answers

Multithread ConcurrentModificationException

I have searched the web for a while now trying to resolve this issue, but have had no success. In my application, I have a large set of messages that I am attempting to encrypt using a basic commutative encryption scheme. Since the sets are large…
Taaam
  • 1,098
  • 3
  • 11
  • 23