The CopyOnWriteArrayList structure could solve your problem (java.util.concurrent).
-
CopyOnWriteArrayLists is thread-safe because all mutative operations are implemented by creating a copy of the list. -
The problem of
ConcurrentModificationExceptionis avoided because the array doesn’t change while iterated. The so calledsnapshot style iteratoruses a reference to the state of the array when the iterator was created. -
If you have much more reads than writes, use
CopyOnWriteArrayList, otherwise useVector. -
Vectorintroduces a small synchronization delay for each operation, whenCopyOnWriteArrayListhas a longer delay for write (due to copying) but no delay for reads. -
Vectorrequires explicit synchronization when you are iterating it (so write operations can’t be executed at the same time),CopyOnWriteArrayListdoesn’t.