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

Working of synchronized methods in Collection class

I try to understand the working of Concurrent modification exception in Java, Initially I tried Concurrent modification exception example with normal ArrayList and HashSet by removing elements while iterating, and then with help of…
-2
votes
1 answer

No ConcurrentModificationException during index based iteration

I have following code: public static void main(String[] args) { List input = new ArrayList<>(); List output = new ArrayList<>(); for(int i=0; i< 1000 ;i++){ input.add(i+""); } for(int i=0 ;…
Kumar
  • 1,536
  • 2
  • 23
  • 33
-2
votes
1 answer

Java ConcurrentModificationException with an ArrayList

I'm creating an online videogame using Java. If have a client and a server application. In the server, to handle the player database, I create an ArrayList named inGamePlayers, that contains Players object (with InetAdress ipAdress and String…
GriffinBabe
  • 146
  • 1
  • 12
-2
votes
2 answers

java threads alternative to synchronization

I am new to concurrent programming and I am facing few issues with the below code using Java threads. Status Class (this class tracks the position availability): public class Status { private static Map positions = new…
Vasu
  • 21,832
  • 11
  • 51
  • 67
-2
votes
1 answer

java8 stream map reduce cause a the ConcurrentModificationException, why?

final BigDecimal couponlessAmount = orderItems.stream() .filter(item -> !item.getIsUseCoupon()) .map(item -> item.getTotalAmount().subtract(item.getReduceProductAmount())) .reduce(BigDecimal.ZERO,…
komite
  • 13
  • 2
-2
votes
1 answer

HashMap's Iterator to prevent concurrent modification

The following piece of code, while executed by a single thread, throws a ConcurrentModificationException on line 4: Map map = new HashMap(); map.put("key1", "value1"); map.put("key2", "value2"); for (String key :…
spongebob
  • 8,370
  • 15
  • 50
  • 83
-2
votes
3 answers

ConcurrentModificationException when edting the data in the arraylist to text file

private void btnOKActionPerformed(java.awt.event.ActionEvent evt) { try { if(txtStaffID.getText(0, 2).equals("AD")) { …
jefferyleo
  • 630
  • 3
  • 17
  • 34
-2
votes
4 answers

Strange ConcurrentModificationException behavior

I don't understand why this method throws an exception: public void add(Object obj){ gameObjects.add(obj); //here the exception happens } ... while this one doesn't: public void add(Object obj){ gameObjects.add(obj); // no exception…
Kacper Lubisz
  • 780
  • 2
  • 10
  • 17
-2
votes
2 answers

ConcurrentModificationException in java

edit: problem solved, it came indeed from the sublist. Thanks! I am doing a project about genetic algorithms. but I have the entitled error, and I don't really know where it happens, so I would need your help... here is the function: public…
-2
votes
2 answers

i got ConcurrentModificationException while dealing with arraylists

i use this code to check the data in an arraylist and remove the similarities but i got a ConcurrentModificationException. this is the final code after solving all the problems: public class Aaa { static ArrayList cmp = new…
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
-2
votes
1 answer

Java Concurrent Modification Exception: How to deal with?

I'm writing a text file crawler program and keep getting a ConcurrentModificationException. I know it has something to do with using Iterator but I'm not sure how to fix it. Please help! Exception is: Exception in thread "main"…
super.rach
  • 23
  • 1
  • 7
-3
votes
1 answer

How can we handle the Java ConcurrentModificationException using try-catch?

I am trying to handle the java ConcurrentModificationException exception using try-catch block but still I am getting the same error when compiling the code. import java.util.*; public class failFast{ public static void main(String[] args){ …
-3
votes
1 answer

Java: concurrentModificationException while iterating on arrayList

I'm trying to practice java's Iterators, and to be more accurate, Im trying to understand the concurrentModificationException. When this exception is thrown? From what I've read, this exception is thrown on any object removal when iterating with a…
Jessi
  • 69
  • 7
-3
votes
1 answer

ConcurrentModificationException logic breakdown

As per docs ConcurrentModificationException states: ConcurrentModificationException occurs when an object is tried to be modified concurrently when it is not permissible. I'm trying to de-rust on some java conceptsand make a huffman compression…
-3
votes
1 answer

how to Continous loop collection without concurrentmodificationexception

Please read the text carefully, the title isn't so good, but i couldn't think something easier to describe the problem. This is a theorical problem, i will use java to demonstrate but the solution i need is more like a design pattern so it could be…
Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
1 2 3
41
42