concurrency - How to handle synchronization of frequent concurrent read/writes on a Java ArrayList -


i have java class contains arraylist of transaction info objects queried , modified different threads on frequent basis. @ basic level, structure of class looks (currently no synchronization present):

class statistics {     private list<traninfo> traninfolist = new arraylist<traninfo>();      // method runs - every time transaction comes in.     void add(traninfo traninfo)     {         traninfolist.add(traninfo);     }      // method acts cleaner , runs occasionally.            void removebasedonsomecondition()     {         // code determine items remove          traninfolist.removeall(listofunwantedtraninfos);     }      // methods query stats on tran info.     // these methods called frequently.     stats getstatsbasedonsomecondition()     {         // iterate on list of tran info         // objects , return stats     }       stats getstatsbasedonsomeothercondition()     {         // iterate on list of tran info         // objects , return stats     } } 

i need ensure read/write operations on list synchronized correctly, however, performance important, don't want end locking in every method call (especially concurrent read operations). i've looked @ following solutions:

copyonwritearraylist

i've looked @ use of copyonwritearraylist prevent concurrentmodificationexceptions being thrown when list modified while iterating on it; problem here copy required each time list modified... seems expensive given how list modified , potential size of list.

readwritelock

a readwritelock used synchronize read/write operations while allowing concurrent read operations take place. while approach work, ends resulting in lot of synchronization code in class (this isn't end of world though).


are there other clever ways of achieving kind of synchronization without big performance penalty, or 1 of above methods recommended way? advice on appreciated.

i'd use collections.synchronizedlist() until know sure indeed crucial performance bottle neck of application (needless doubt ;-)). can know sure through thorough testing. assume know "premature optimization"...

if strive optimize access list i'd readwritelock approach.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -