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

Multiple threads accessing one variable

I found this question in a textbook I am reading. The solution is given below it as well. I'm having trouble understanding how the minimum could be 2. Why couldn't a thread read 0, all other threads execute and it writes 1? And whether it is 1 or 2,…
John
  • 3,037
  • 8
  • 36
  • 68
5
votes
1 answer

Why doesn't my sample throw ConcurrentModificationException

I wrote this example following a test ConcurrentModificationException concept: public class Person { String name; public Person(String name) { this.name = name; } } public static void main(String[] args) { List l…
Sam
  • 6,770
  • 7
  • 50
  • 91
5
votes
3 answers

Concurrent Hashmap - Fail safe issue

I was trying an example for Fail-Safe using ConcurrentHashMap. Below is the sample snippet which I tried.. ConcurrentHashMap cMap = new ConcurrentHashMap(); cMap.put("1", "Windows Phone"); cMap.put("2",…
Kaushi
  • 198
  • 3
  • 8
  • 20
5
votes
3 answers

Java Observer Pattern - How to remove observers during update(notify) loop/iteration?

I am very new to java so sorry in advance if anything I say sounds newbish, be gentle. I have implemented a basic Observer Pattern. Some observers should only listen to one update and then immediately remove themselves from the observers/listeners…
SportySpice
  • 335
  • 5
  • 10
5
votes
3 answers

ConcurrentModificationException even with using Collections.sychronizedMap on a LinkedHashMap

I'm using a Map object in my class that I've synchronized with Collections.synchronizedMap() for a LinkedHashMap like so: private GameObjectManager(){ gameObjects = Collections.synchronizedMap(new LinkedHashMap()); } I'm…
5
votes
3 answers

How to use two different iterators on a Linked List in Java?

I would like to use a linked list in order to perform extractions and insertions of elements, trying out all combinations for a heuristic. Linked lists are more efficient for this type of operations. Since I would want to try all possible pairs of…
5
votes
1 answer

fail fast behaviour of java HashMap

I played around with java.util.HashMap to learn what the fail-fast behaviour is. HashMap map = new HashMap(); map.put("jon", 10); map.put("sean", 11); map.put("jim", 12); map.put("stark", 13); map.put("vic", 14); Set keys = map.keySet(); for(Object…
damon
  • 8,127
  • 17
  • 69
  • 114
5
votes
3 answers

Concurrent modification of a list while using copy constructor

Will the following code cause a ConcurrentModificationException or other side effects? ArrayList newList = new ArrayList(list); Considering that the size of list is very huge and another thread is concurrently modifying the list…
Sushant
  • 379
  • 3
  • 14
5
votes
2 answers

Best way to prevent concurrent modification exception

Here is some pseudo code as follows. public class MyObject { private List someStuff; private Timer timer; public MyObject() { someStuff = new ArrayList(); timer = new Timer(new TimerTask(){ …
thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
4
votes
2 answers

Java Programming Error: java.util.ConcurrentModificationException

I'm writing a program as part of tutorial for a beginner Java student. I have the following method and whenever I run it, it gives me the following exception: java.util.ConcurrentModificationException at…
user1104775
  • 259
  • 1
  • 3
  • 7
4
votes
2 answers

Best practices for trapping a unhandle runtime Exception for JAVA on the Android os?

During a process that I have firing off during a e3roid scene population on the android I keep coming across exceptions that I want to completely trap. Perhaps I need to create a back exception tracker that i can transverse through at my leisure…
Lenn Dolling
  • 1,300
  • 13
  • 23
4
votes
3 answers

ConcurrentModificationException (Java)

Exception in thread "main" java.util.ConcurrentModificationException Squash the PC dirties the room Violet. The room's state is now dirty Lily the animal growls The Animal Lily left the room and goes to Green through the west door. at…
Kirs Kringle
  • 849
  • 2
  • 11
  • 26
4
votes
1 answer

When do we (or when do we not) allow concurrent modification when using ListIterator?

For instance, let's say there is a some Collections#reverse(List) operation that uses the ListIterator as such: var forwardItr = list.listIterator(); var reverseItr = list.listIterator(list.size()); while (forwardItr.nextIndex() <…
bizness86
  • 57
  • 7
4
votes
2 answers

Consistency for simultaneous UPDATES in Oracle, when the WHERE clause depends on the old value

I've been reading about Oracle data consistency guarantees, and supported transaction isolation levels, (e.g. here: https://docs.oracle.com/database/121/CNCPT/consist.htm#CNCPT121) and I feel like I'm getting a lot of high-level information, but I'm…
4
votes
2 answers

java.util.ConcurrentModificationException on ArrayList

I have a Server class and a Timer inside it which is supposed to clear dead clients (clients who crashed). I followed the example below by locking the collection when the Timer iterates over the users but I still get this exception (after I crash a…
Lightforce
  • 61
  • 1
  • 2
  • 5