This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

thread - ConcurrentModificationException  RSS feed

 
Cunces Corten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm making a game but sometimes I get exceptions.

So in my main class I have a keylistener which starts a thread when the user fires the first time.

1) Main class





So according to me this can't throw java.util.ConcurrentModificationException since I'm using an iterator. When I click on the error it says the error starts here: Bullet b = i.next();

Can someone tell me how this is possible?
btw this exception occurs when I enter spacebar for a long time.. lol
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cunces,

Next time you post code, could you put your code in [code][/code] tags? It makes it a lot more readable. There is a button at the top of the message form you can use for adding the tags for you. You can also go back and edit your post to add them in.

To your question, the cocnurrent modification exception can occur if another thread modifies the list while this thread iterates over it. In your case, you have a different part of code which adds to the list which may happen when the list is being iterated over, so the problem can occur.

To get around this you should use a thread-safe collection which would prevent the ConcurrentModificationException. Take a look at the collections available in the java.util.concurrent package. ConcurrentLinkedQueue might work alright for you, or perhaps CopyOnWriteArrayList.
 
Cunces Corten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Hi Cunces,

Next time you post code, could you put your code in [code][/code] tags? It makes it a lot more readable. There is a button at the top of the message form you can use for adding the tags for you. You can also go back and edit your post to add them in.

To your question, the cocnurrent modification exception can occur if another thread modifies the list while this thread iterates over it. In your case, you have a different part of code which adds to the list which may happen when the list is being iterated over, so the problem can occur.

To get around this you should use a thread-safe collection which would prevent the ConcurrentModificationException. Take a look at the collections available in the java.util.concurrent package. ConcurrentLinkedQueue might work alright for you, or perhaps CopyOnWriteArrayList.


oké forgot to put in code tags again. Thanks for your post, going to check it out :thumbup:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!