java - ConcurrentModificationException without modifying object -
i've got following piece of code causing concurrentmodificationexception. i'm not modifying products
object @ all,
list<product> products = product.findactivebyfilter(filters); set<long> temp = new hashset<long>(); list<product> resultsettemp = new arraylist<product>(); (product product : products) { // << exception points if(!temp.contains(product.getid())){ temp.add(product.getid()); resultsettemp.add(product); } } products.clear(); products.addall(resultsettemp);
i've seen exception pop-up several times, cannot reproduce (it happens randomly).
product.findactivebyfilter
method returns new instance of list<product>
has been build cached list<product>
.
edit: i've found way reproduce error. code called when client wants products (its webshop), , website loads more items when client scrolls down. triggers exception (as server not-yet done responding products, , gets call it). race conditions, fun!
as said already, caused seperate tread 'modifying' products
(as cached instance). i've changed implementation of product.findactivebyfilter
return new arraylist<product>(products);
instead of reference cached value, when no filters applied (thus no filtered result given back).
public static list<product> findactivebyfilter(arraylist<filterpair> filters) { list<product> products = getcachedallproductsbyfirstsupplier(); if (products == null) { products = new arraylist<product>(); } if(filters.size() > 0) { list<product> productsfiltered = new arraylist<product>(products); // ... many checks here filters ... return productsfiltered; } return new arraylist<product>(products); // not give cached copy, 'return products;' }
there 2 calls findactivebyfilter, called website. first 1 did include filter, second 1 did not (so first 1 still busy, while second 1 returned directly).
Comments
Post a Comment