CopyOnwriteArrayList alternative for frequent updates

please feel free to correct me.

A script to create a marketdata cache that requires temporal ordering (i.e., ordered by temporal elements). The cache will have many updates.

Logic dictates that a list is a logical choice that allows indexing to freely and easily do things like getOldestElem list.get (0); binary search etc.

This cache will be used by several threads that explicitly look at and update the cache that points to CopyOnWriteArrayList. (Javadoc points out that this is not suitable for large sizes and frequent updates)

Am I looking for an alternative or a better idea?

+4
source share
2 answers

If exisitng records are rarely (or never) updated, so frequent updates are often frequent additions, you can use temporary ordering to optimize the data structure by separating old and new elements.

For example, you can segment the cache into several CopyOnWriteArrayList objects, each of which contains no more than a fixed number of elements, and each segment contains a temporary data sub-range. Frequent additions would increase the last segment to the maximum size, after which you would add a new segment for future updates. CopyOnWriteArrayList maximum segment size, you should avoid CopyOnWriteArrayList performance CopyOnWriteArrayList . You must hide implementation details in your own List class.


Edit: Deleting old elements similarly changes only one segment: the oldest segment.

+3
source

It is best to check your use case, but an ReadWriteLock protected ArrayList can give you better performance.

+2
source

All Articles