This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

thread - ConcurrentModificationException

 
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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • 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:
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic