When you have two Thread
s that share the same ArrayList
and one thread iterates over the ArrayList
while the other updates the ArrayList
you can get a ConcurrentModificationException
.
E.g.
Collection<Integer> coll = new ArrayList<Integer>();
Thread1 :
for(Integer i : coll){
i++;
}
Thread2 :
coll.add(12);
My question is: are there tools which can analyse that no such errors exist in a code base? I know this means you have to be able to resolve that variables in different part of the code base refer to the same collection at runtime and it may be a hard problem to solve in general.
But I'm working on a program where it is easy to identify that the variables refer to the same Collection
s.
Are there analysers which can warn me of this kind of potential ConcurrentModificationException
(on ArrayList
, HashMap
) or can you give me advice on which tool, libraries to use to check this in my specific context where I can easily see which variables are shared between threads.
I'm looking at JavaParser
right now but this doesn't seem trivial.