java - Concurrent container access? -


right i'm experimenting basic game concepts in java. @ moment, display 2 boxes: 1 player can move arrow keys, , "enemy" jitters around randomly. right now, i'm fixing player's ability shoot little squares in direction of mouse whenever click left mouse button. it's structured right now, have 4 classes - jpanel holds "game" logic, jframe holds panel, entity class simple representation of physical object position, size, , speed methods moving it, , projectile class extends entity class special move method moves 1 step along path calculated initial , destination points given projectile's constructor.

the problem i'm having trying update position of projectiles. have timer running update positions of non-player controlled objects, , actionperformed() method of jpanel has call every existing projectile's move() method. right now, when projectile created put projectile array arbitrarily large size, making sure start @ [0] when array full. actionperformed() method can safely act on array concurrent mouse action listener, since i'm working on more learn settle simple solution works, i'm wondering if there's better way having array size [999]. tried using arraylist @ first, ended concurrent access errors. tried queueing projectiles in array , adding them arraylist in actionperformed() method, gave me null pointer errors.

(tldr) question: of java containers (set, map, queue, etc) support concurrent access? don't think need container order matters, since have iterate on every element in container, need container of dynamic size allows 1 thread add objects , remove/call objects concurrently.

"i'm wondering if there's better way having array size [999]. tried using arraylist @ first, ended concurrent access errors."

you can use arraylist. getting concurrentmodificationexception because may trying remove object arraylist while looping through it. can see similar question here.

the solution use iterator instead of loop, iteration through objects.

from iterator api:

  • iterators allow caller remove elements underlying collection during iteration well-defined semantics.

then can use iterator.remove() method, when want remove object arraylist. have projectiles have in arraylist, this

list<projectile> projectiles = new arraylist<>(); ... iterator = projectiles.iterator(); while (it.hasnext()) {     projectile projectile = (projectile)it.next();     if (projectile.hitenemy()) {         it.remove();     } } 

the it.remove() remove current projectile in iteration. can't done in each loop, concurrentmodificationexception.

you can see more how use iterators in the collection interface tutorial. can read more concurrentmodificationexception in this thread


see this answer where, while iterating through list in timer, if asteroid off screen, removed list dynamically through iterator.remove() method.

enter image description here


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -