java - Is there an alternative to CopyOnWriteArrayList which can be sorted? -


i have collection of 'effects' draw on 'object' in gui ( gradients, textures, text etc ). nature of underlying system means effect collection can accessed multiple threads. majority of operations reads, @ moment i'm using copyonwritearraylist works ok.

but need sort collection of effects based on draw order whenever add new effect or change effect's draw order. need able iterate through collection in forwards & reverse ( iterater.next() & iterator.previous() ).

after research i've found copyonwritearraylists don't being sorted:

behaviour of copyonwritearraylist

if tried sort copyonwritearraylist you'll see list throws usupportedoperationexception (the sort invokes set on collection n times). should use read when doing upwards of 90+% reads.

i found suggestion of using concurrentskiplistset, handles concurrency & sorting, looking @ javadoc i'm worried this:

beware that, unlike in collections, size method not constant-time operation.

and use & rely on size() method quite bit.

i implement system of manual synchronization on effect collection standard arraylist, big refactor & i'd rather exhaust other possibilities, if has ideas? sticking me far.

probably best way go manually synchronize @ point sorting collection. (pseudo code) :

synchronize {   convert copyonwritearrylist normal list.  sort normallist.  convert normal list copyonwritearraylist , replace shared instance   } 

alternatively might use normal arraylist , roll out own solution using reentrantreadwritelock should work ok in case have more reads writes.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -