java - Problems with two threads accessing a frequently updated Arraylist -


i have arraylists store many objects, , objects added , removed arraylists. 1 thread operates on data structures , updates arraylist's objects every 20ms or so. thread traverses arraylists , uses elements paint objects (also every 20-30ms).

if arraylists traversed using loop, indexoutofboundsexceptions abound. if arraylists traversed using iterators, concurrentmodificationexceptions abound. synchronizing arraylists so:

 list list = collections.synchronizedlist(new arraylist()); synchronized(list) { //use iterator traversals } 

throws no exceptions has substantial performance drain. there way traverse these arraylists without exceptions being throw, , without performance drain?

thanks!

a approach problem make threads work on different copies of list. however, copyonwritearraylist doesn't fit here well, since creates copy @ every modification, in case better create copies less frequent.

so, can implement manually: first thread creates copy of updated list , publishes via volatile variable, second thread works copy (i assume first thread modifies list, not objects in it):

private volatile list publiclist;  // thread list originallist = ...; while (true) {     modifylist(originallist); // modify list     publiclist = new arraylist(originallist); // pusblish copy }  // thread b while (true) {     (object o: publiclist) { // iterate on published copy         ...     } } 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -