alternative to CopyOnWriteArrayList for frequent writes, occasional iterating

I have an ArrayList that is to be cached and shared across multiple threads indefinitely. Operations include frequent adds and removes, plus occasional iterating over it.

The ArrayList lives in a wrapper class which manages access to it:

public class MyListWrapper implements Iterable {

    private List innerList = new ArrayList();

    public Iterator iterator() {
        return innerList.listIterator();
    }

    public void add(T element) {
        innerList.add(element);
        //app-specific logic
    }

    //remove(T), etc in the same pattern...
}

I'm currently making preparations for thread safety. At first, CopyOnWriteArrayList seemed like the best answer, but its performance concerns me, since modifications will be made more often than anything else.

Would a manual change to the wrapper class such as this be a better alternative?:

public Iterator iterator() {
    return new ArrayList(innerList).listIterator();
}

//plus concurrency tweaks for any non-atomic modifications to innerList

Please help me find the best approach.

14
задан Paul Bellora 29 October 2016 в 22:58
поделиться