简体   繁体   中英

Does Java have a data structure that supports Concurrent Modification?

I'm making a game in Java. Every enemy in the game is a thread and they are constantly looping through the game's data structures (I always use the class Vector).

Lately I have been getting the "ConcurrentModificationException" because an element is being added/removed from a Vector while a Thread is looping through it. I know there are strategies to avoid the add/remove problem (I actually use some to avoid problems with Removes but I still have problems with "Adds").

I heard that java supports a Vector/List that avoids the ConcurrentModificationException.

Do you have any idea of what this structure might be? Thanks.

查看java.util.concurrent ,它有你想要的。

CopyOnWriteArrayList . But read its javadocs carefully and consider if in practice it gives the behavior that you are expecting (check Memory Consistence effects), plus if the performance overhead is worth it. Besides synchronization with ReentrantReadWriteLock , AtomicReferences , and Collections.synchronizedList may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM