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

is postgres UPSERT fully atomic/thread safe?

We're trying to stream data to postgres 11, using the following query: INSERT INTO identifier_to_item values (:id, :identifier_value, :identifier_type, :identifier_manufacturer, :delivery_timestamp_utc, :item) ON CONFLICT (identifier_value,…
2
votes
2 answers

Couchbase server allowing concurrent document mutation, even with CAS set?

I have an object, SomeObject, which represents an object stored as a document in Couchbase. SomeObject has a cas variable for containing the CAS value. I have code like this: /* Get two identical objects from Couchbase, they'll have identical CAS…
A.C.
  • 53
  • 1
  • 6
2
votes
1 answer

I have a 2 concurrent data structures and I want to update them atomically

How to make sure that these two concurrent data structure statements get executed as an atomic block without using synchronized statement so that the sum is consistent at any given point in time? Note : I am new to multi-threading in…
Sonu
  • 33
  • 5
2
votes
0 answers

Android: ConcurrentModificationException error

I'm scratching my head here trying to understand why I'm getting a ConcurrentModificationException here... This is my code: var adapterCurrentList : PagedList? = null val workPackagesArrayList =…
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
2
votes
2 answers

Is a non-synchronized WeakHashMap harmful?

I have a code look like this. private static Map PATTERNS; private static Map patterns() { if (PATTERNS == null) { PATTERNS = new WeakHashMap<>(); // ok? or should be synchronized? } return…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
2
votes
1 answer

Multi-threaded (Parallel) Access to Built-in Common Lisp Objects

The topic of multi-threaded access to Lisp objects came up in another post at https://stackoverflow.com/posts/comments/97440894?noredirect=1, but as a side issue, and I am hoping for further clarification. In general, Lisp functions (and special…
davypough
  • 1,847
  • 11
  • 21
2
votes
4 answers

How to remove elements from a HashMap without getting ConcurrentModificationException

I am comparing every entry in a HashMap to every other entry in the same HashMap. While iterating the HashMap I remove some elements based on some conditions. However I keep getting the ConcurrentModificationException. Iterator
sart
  • 33
  • 1
  • 9
2
votes
2 answers

Can get, put & remove element in HashMap without iteration cause ConcurrentModificationException?

I have a static hashMap, shared with multiple threads. I am not iterating the map at all but just uses the get, put, remove. Is it safe from ConcurrentModificationException ? The method looks like this private static Map TRACKER = new…
Anthony C
  • 2,117
  • 2
  • 15
  • 26
2
votes
0 answers

How to handle concurrent inserts with multiple JVMs?

We have a Spring Boot application with a controller class that is called from the clients and interacts with the DB. Based on a condition, the controller checks if a record for a unique value exists or not, if not, it creates a new record (with…
2
votes
2 answers

MongoDB Concurrent read/write on single document

I'm having an issue with concurrent reads and writes on MongoDB, and was wondering if there was a simple solution embedded within the DB system. I have an object that collects statistics for an app, and is represented on MongoDB as a single document…
Jo Colina
  • 1,870
  • 7
  • 28
  • 46
2
votes
1 answer

What is the most elegant way to remove a listener from a list from a callback

Suppose I have the following listener interface MyListener { fun onResult(result: Int) } and that my class holds a list of this listener val MyListenerList = ArrayList() My doubt is: if someone that registered the listener wants to…
Augusto Carmo
  • 4,386
  • 2
  • 28
  • 61
2
votes
2 answers

Vector throws ConcurrentModificationException despite being synchronized

I had an ArrayList that was being operated on by multiple threads, which wasn't working as the ArrayList isn't synchronized. I switched the list to a Vector as instructed by my professor. Vector is synchronized, but I'm having exceptions thrown…
brienna
  • 1,415
  • 1
  • 18
  • 45
2
votes
1 answer

Java concurrent modification exception is not coming while using collections remove method

I have seen in many places that it says when removing an element from an ArrayList while iterating, I should be using iterator remove method instead of collections remove method to avoid concurrent modification exception. However, below code works…
KItis
  • 5,476
  • 19
  • 64
  • 112
2
votes
2 answers

Why am I getting a java.util.ConcurrentModificationException in this example?

In the following code sample, I don't understand why foo method throws ConcurrentModificationException. Please help! private void foo() { synchronized (map) { if (map != null && !map.isEmpty()) { Set it =…
phnmnn
  • 12,813
  • 11
  • 47
  • 64
2
votes
2 answers

Why isn't ConcurrentModificationException being thrown here?

Look at this little piece of code: ArrayList al = new ArrayList(); al.add("AA"); al.add("AB"); al.add("AC"); Iterator it = al.iterator(); while(it.hasNext()){ String s = (String)it.next(); if(s.equals("AB")){ al.remove(1); } } Since ArrayList has…
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66