PokeGOAPI-Java icon indicating copy to clipboard operation
PokeGOAPI-Java copied to clipboard

Threading issues

Open pvanassen opened this issue 10 years ago • 6 comments

Because regular lists are being used and those lists are cleared instead of e.g. instantiating a new list, I run into a bunch of errors like these:

java.util.ConcurrentModificationException: null
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1380)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)

Either use CopyOnWriteArrayList or set new Array lists instead of clearing the old ones

pvanassen avatar Aug 09 '16 09:08 pvanassen

Or wrap them in a Collections.synchronizedList(new ArrayList<>());

Jari27 avatar Aug 09 '16 12:08 Jari27

No not synchronised lists :(. That makes everything unnecessary slow. Instead of a clean a new object would be better since object creation is a lot cheeper than synchronising everything. GC should take care of the old object of the user does not hang on to it

pvanassen avatar Aug 09 '16 12:08 pvanassen

Or just wrap every return in new ArrayList<>() is better than synchronising them

pvanassen avatar Aug 09 '16 12:08 pvanassen

Syncd lists dont stop concurrent modifications anyway

Grover-c13 avatar Aug 09 '16 12:08 Grover-c13

Oh, mm. I thought they were expressly made for that. It's a slow implementation, that is true. But we don't have that many concurrent modifications (at most one) so the lock wouldn't be very long.

Jari27 avatar Aug 09 '16 12:08 Jari27

You should maybe consider using a ConcurrentLinkedQueue (https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html)

Tarnadas avatar Aug 09 '16 19:08 Tarnadas