Is there an alternative to CopyOnWriteArrayList which can be sorted?

I have a collection of 'effects' I draw on an 'object' in a GUI ( gradients, textures, text etc ). The nature of the underlying system means that this effect collection can be accessed by multiple threads. The majority of operations are reads, so at the moment I'm using a CopyOnWriteArrayList which works ok.

But now I need to sort the collection of effects based on their draw order whenever I add a new effect or change an effect's draw order. I also need to be able to iterate through the collection in forwards & reverse ( iterater.next() & iterator.previous() ).

After some research I've found CopyOnWriteArrayLists don't like being sorted:

Behaviour of CopyOnWriteArrayList

If you tried to sort a CopyOnWriteArrayList you'll see the list throws an UsupportedOperationException (the sort invokes set on the collection N times). You should only use this read when you are doing upwards of 90+% reads.

I also found a suggestion of using ConcurrentSkipListSet, as it handles concurrency & sorting, but looking at the JavaDoc I'm worried about this:

Beware that, unlike in most collections, the size method is not a constant-time operation.

And I use & rely on the size() method quite a bit.

I could implement a system of manual synchronization on the effect collection as a standard ArrayList, but this is a very big refactor & I'd rather exhaust all other possibilities, if anyone has any ideas? Thanks for sticking with me this far.


Probably the best way to go is to manually synchronize at the point where you are sorting your collection. You could do something like (pseudo code) :

synchronize { 

convert copyonwritearrylist to normal list. 
sort normallist. 
convert normal list to copyonwritearraylist and replace the shared instance 

}

Alternatively you might just use a normal ArrayList and roll out your own solution using ReentrantReadWriteLock This should work OK in case you have more reads than writes.

链接地址: http://www.djcxy.com/p/76112.html

上一篇: CopyOnWriteArrayList实现问题

下一篇: 是否有可以排序的CopyOnWriteArrayList的替代方法?