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
3
votes
3 answers

How Concurrent modification exception is handled internally by CopyOnWriteArrayList/ConcurrentHashMap?

I want to understand internally how concurrent modification exception is handled in concurrent collections like ConcurrentHashMap and CopyOnWriteArrayList. There are so many blogs available in internet which suggest to use these two data structures…
3
votes
2 answers

java.util.ConcurrentModificationException -- bug when performing IO operations (not with a list)

Ok. Here is the scenario. I have a form that the user fills out to create a Match object. I use an IntentService to write the information to file on a background thread. If a boolean of "true" is passed in the intent, then a corresponding ScoreFile…
Jo Momma
  • 1,126
  • 15
  • 31
3
votes
3 answers

ConcurrentModificationException when using iterator to remove entry

I have a simple piece of code that loops through a map, checks a condition for each entry, and executes a method on the entry if that condition is true. After that the entry is removed from the map. To delete an entry from the map I use an Iterator…
3
votes
4 answers

Two iterators throw ConcurrentModificationException

I have the following code public static void main(String[] args) { List list = new ArrayList<>(); Arrays.stream("hello how are you".split(" ")).forEach(s -> list.add(s)); Iterator it = list.iterator(); …
v1shnu
  • 2,211
  • 8
  • 39
  • 68
3
votes
1 answer

Why can't I execute truncate and grant statement on a table in parallel?

My use case is that I need to execute GRANT and TRUNCATE statements on a table concurrently. Sample scenario: When I try to execute below statements in parallel(two separate terminals): while true; do psql -U -d -c 'GRANT select ON…
Jyoti Dhiman
  • 540
  • 2
  • 6
  • 17
3
votes
2 answers

ConcurrentModification Exception on Sorting

I wrote this small program to sort arrays. To my understanding it should print 0,1,2. However, when I run this program I am getting ConcurrentModificationException public class Test { public static void main(String[] args) { List
T-Bag
  • 10,916
  • 3
  • 54
  • 118
3
votes
2 answers

How do disk controllers handle concurrent writes to same sector in absence of write barriers?

When I open a file with O_DIRECT|O_ASYNC and do two concurrent writes to the same disk sector, without a fsync or fdatasync in between, does the linux disk subsystem or the Hardware disk controllers offer any guarantee that the final data on that…
3
votes
3 answers

A concurrent collection that maintains insertion order

I'm looking for a concurrent list that can maintain the insertion order. Does anyone have some good recommendation ? I look at some from guava e.g. SetFromMap, but they are deprecated in the new version. Thank you.
Xitrum
  • 7,765
  • 26
  • 90
  • 126
3
votes
1 answer

Java modifying list concurrently at different places

I have this piece of code where I am traversing an ArrayList using an iterator, like: Iterator iterator = list.iterator(); while (iterator.hasNext()) { Element element = iterator.next(); iterator.remove(); handle(element) } where…
3
votes
2 answers

ArrayList 'squash' operation on its elements

I have a problem that seems to be trivial but I'm looking for the best way of resolving that issue. Let's say I have a class: Product.class public class Product { private int id; private String code; private String price; private…
3
votes
2 answers

Delete elements from Hashmap while iterating over it

Here is my code. I have an arraylist of visited elements. So I want delete these visited elements from the hashmap and below is code. It gives me concurrentmodification exception. private static void removeVisitedNodes(ArrayList arrayList)…
maddie
  • 629
  • 10
  • 29
3
votes
1 answer

SortedSet::removeAll( headSet ) fails, but deriving another collection from headSet succeeds. Why?

From a TreeSet (a SortedSet) in Java 8: I call the ::headSet method to get a SortedSet of the objects at the front of the sorted collection. I call ::removeAll to delete those frontmost objects.BAM, throws a ConcurrentModificationException. Yet…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3
votes
1 answer

Do Python 2.7 views, for/in, and modification work well together?

The Python docs give warnings about trying to modify a dict while iterating over it. Does this apply to views? I understand that views are "live" in the sense that if you change the underlying dict, the view automatically reflects the change. I'm…
Ouroborus
  • 16,237
  • 4
  • 39
  • 62
3
votes
2 answers

how to avoid ConcurrentModificationException from JFileChooser.setFileFilter() call?

I have the following code and just got ConcurrentModificationException. fchProtocol = new FileChooser(lastFileLoc); FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("xml files (*.xml)", "xml"); fchProtocol.setFileFilter(xmlfilter); …
peterboston
  • 877
  • 1
  • 12
  • 24
3
votes
2 answers

Java Concurrency Issues concerning Listeners

What is the best, or the common solution to concurrency concerning Java? I see a lot of solutions on this and other websites, but none of them work for my problem. My problem is as follows: I have a vector of a class I wrote myself. (If this helps…
Lara
  • 2,594
  • 4
  • 24
  • 36