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
4 answers

Exception while removing from list used by thread

thats part of my code: public List _list = new ArrayList<>(); public void removeInteger(Integer i) { _list.remove(i); } public class CheckThread implements Runnable { @Override public void run() { …
0
votes
1 answer

Is ConcurrentSkipListSet static?

I got a concurrentModificatoin Exception while working over a HashSet and hence shiftedover to ConcurrentSkipListSet. Although the concurrency issue is solved, i now have a new problem. It seems that a ConcurrentSkipListSet is static by default.Is…
0
votes
3 answers

Synchronizing access to ArrayList

I am trying to calculate all the primes up to an arbitrary number by computing them in parallel. I'm aware that isPrime() can be improved from what it is now, but the main problem is that I can't synchronize access to the ArrayList that is storing…
xst
  • 2,536
  • 5
  • 29
  • 41
0
votes
2 answers

concurrent update with hibernate

Suppose user_1 and user_2 accessed an employee with id = 101, name = Rahul, accountBalance = 1500 at the same instant. so both user have employee with above mentioned values in their hand now. User_1 added a 1000 to accountBalance and updated…
amFroz
  • 95
  • 1
  • 12
0
votes
7 answers

How can i counter a ConcurrentModificationException?

if have the following problem: I have a List which i am going through using the enhanced for loop. Every time i want to remove sth, out of the list, i get a ConcurrentModificationException. I already found out why this exception is thrown, but i…
Leander
  • 1,322
  • 4
  • 16
  • 31
0
votes
1 answer

Will iterator be damaged if remove() called but did nothing?

Suppose I am iterating over some collection, then call remove() with absent key so that it does nothing. Will next iteration cause exception?
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
0
votes
4 answers

arraylist concurrent modification

I'm creating a multithread chat in java. When user u1 sends a message to user u2 but user u2 is not connected, user u1 sends the message to the server and user u2 will receive the message once he connects to the server. The messages who are not sent…
tony danza
  • 306
  • 7
  • 22
-1
votes
1 answer

Java - ConcurrentModificationException

The following piece of code throws a ConcurrentModificationException almost every time it is called. The second piece of code does not throw the exception, however it is not the correct logic that I need. If the object is an instance of EditorFrame,…
Jakir00
  • 2,023
  • 4
  • 20
  • 32
-1
votes
1 answer

Is modCount an atomic variable?

I read that a ConcurrentModificationException can be thrown whenever a thread performs a structural modification upon a certain list while another thread is iterating over its elements. To detect such modification, instances of the class List store…
Mehdi Charife
  • 722
  • 1
  • 7
  • 22
-1
votes
1 answer

ConcurrentModificationException using Collectors.toSet()

I have a Set that I want to filter by class to obtain a Set (i.e., the subset of Objects that are instanceof Foo). To do this with Java-8 I wrote Set filtered = initialSet.parallelStream().filter(x -> (x instanceof Foo)).map(x ->…
LJ in NJ
  • 196
  • 1
  • 1
  • 12
-1
votes
1 answer

ConcurrentModificationError when traversing a tree recursively

When traversing a tree structure recursively in order to calculate weights and volumes for an entire bill of materials, I run into a ConcurrentModificationException. My approach in pseudocode: Query initialization: add root node to list of nodes…
-1
votes
2 answers

Why this code doesn't throw ConcurrentModificationException when multiple threads work on same arraylist at the same time using iterator

Here 2 threads work on same arraylist and one thread read the elements and another thread remove a specific element . I expect this to throw ConcurrentModificationException . But it is not throwing why? import java.util.ArrayList; import…
-1
votes
2 answers

In Restful web service deployed in a standalone non cluster environment, do we need to use thread safe data structures?

I have a Restful webservice deployed in a non cluster standalone setup.. As we know multiple threads will be automatically spawned to cater to the requests from multiple clients to any of the endpoints defined in this webservice.. Note that there is…
Rahul
  • 89
  • 1
  • 1
  • 7
-1
votes
2 answers

How to Avoid ConcurrentModificationException in that case?

I understand that when I try to modify (add in that case) the list I got ConcurrentModificationException, but what is the best solution to fix that? for (Map.Entry entry : children.entrySet() { childEvent.child =…
Sami
  • 2,311
  • 13
  • 46
  • 80
-1
votes
1 answer

Changing MultivalueMap keys throws ConcurrentModificationException

I have MultivalueMap and a TreeMap, both have key-value of type String. I wish to change any key in my MultivalueMap to keys found in my TreeMap. This is my code, why do I get ConcurrentModificationException and how to solve it? public…
Tal Angel
  • 1,301
  • 3
  • 29
  • 63