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

Concurrent modification error while editing XML

I am getting an concurrent modification error while editing an XML file using Java in a JSP file. How is this caused and how can I solve it? ElementFilter f = new ElementFilter("rurl-link"); Iterator subchilditr = childNode.getDescendants(f); while…
Maverick
  • 2,738
  • 24
  • 91
  • 157
-1
votes
1 answer

ConcurrentModification while adding element into ArrayList

I'm a beginner in Java and I have a problem with one task. I'm trying to add an element to ArrayList but there is an exception: Exception in thread "main" java.util.ConcurrentModificationException public Anagrams (String path) throws…
-1
votes
1 answer

Modifying ConcurrentBag objects from another thread

I have a thread #1 which is looping continuously with foreach through a ConcurrentBag list and modifying the items, but at some time another thread #2 needs to modify an item from the list. Is it safe if I take out all the objects from thread #2…
Mario
  • 13,941
  • 20
  • 54
  • 110
-1
votes
1 answer

How does an Iterator in Java know when to throw ConcurrentModification Exception

I have the following code which throws ConcurrentModificationException because I am using two different iterators on the same list and one of them is modifying the list. So, the second iterator throws the exception when reading the list because some…
qrius
  • 621
  • 2
  • 9
  • 22
-1
votes
1 answer

Need to understand ConcurrentUpdateException after sorting?

I wrote a program which is throwing ConcurrentUpdateException , I am unable to understand this behavior, It should not through any exception as i have only single thread to work with and i have not taken any iterator. What happens in, sorting ? Why…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
-1
votes
1 answer

Is there a thread safe alternative to using iterator to make changes to a list/set?

Here is my code: class Processor implements Runnable { private int id; private Integer interaction; private Set subset; public volatile static AtomicBoolean notRemoved = new AtomicBoolean(true); public Object dcp; …
-1
votes
2 answers

Can Collection.size() throw ConcurrentModificationException?

As we all know, iterating without synchronization on a collection risks throwing ConcurrentModificationException if another thread happens to concurrently modify the collection. But what about calling size() on the collection? Does size() involve…
ARX
  • 1,040
  • 2
  • 14
  • 20
-1
votes
1 answer

ConcurrentModificationException in iterator.next() Multithreaded

I have created this simple worker thread to calculate palindromes by iterating through an ArrayList. I get an Error when I execute line temp_str1 = it.next();. The ArrayList buffer_List is not used by any other thread hence using synchronized block…
-1
votes
1 answer

concurrent modification exception linked hash map android

I am using iterator to delete item from linked hash map but getting concurrent modification exception Iterator it = linkedMap.keySet().iterator(); while (it.hasNext()) { java.lang.Integer key = it.next(); if…
-1
votes
2 answers

When will be concurrent modification exception will be thrown and how iterator remove method will work?

According to the javadocs,conncurrent modification exception will be thrown when we will try to structurally modify the collection while iterating over it.Only iterator remove method will not throw concurrent modification exception. I have analyzed…
mahan07
  • 887
  • 4
  • 14
  • 32
-1
votes
1 answer

ConcurrentModificationException in iterator.next() I cannot find another solution

I'm currently coding a little net thing and wanted to cicle threw all of my players so I used ArrayLists and Lists but there were alot of Exception so going deep to the problem I replaced them with Iterators but I'm getting Concurrent modification…
-1
votes
4 answers

ConcurrentModificationException while trying to remove an element while iterating

I am writing a code that determines if an item can be eaten. If it is edible, I return a give message. Here is my code: public void eat(String item){ //update the game's message with one of the following options: //1:"you are not holding an…
Deborah_Watson
  • 277
  • 1
  • 2
  • 8
-1
votes
3 answers

ConcurrentModificationException when iterating through connections

I have a program emulating a Neural Network, that when finished will evolve it via NEAT algorithm. Neural networks work by having a load of neurons, connected by connections. Part of evolving the program is to cross over neural networks, basically…
dodo
  • 156
  • 10
-1
votes
1 answer

concurrent modification on arraylist

There are a lot of concurrent mod exception questions, but I'm unable to find an answer that has helped me resolve my issue. If you find an answer that does, please supply a link instead of just down voting. So I originally got a concurrent mod…
Mercutio
  • 1,152
  • 1
  • 14
  • 33
-1
votes
2 answers

Exception in thread "main" java.util.ConcurrentModificationException (Head first java book example)

I tried to run a example from Head first java book - second edition (page 152). Its a game puzzle. Which targets Dotcoms(string) in grid to sunk them. But when I tried to run this puzzle I got an Java.util.concurrentModificationException error. I…