Java 8 stream from modified collection

Let's say that I have List

objects that are modified by only one thread (a thread can add or remove objects) and another thread sometimes uses the api thread from the above set to do some manual manipulation on it and on end (impatient operation) return a new collection ... Is this script thread safe? because only one thread updates the collection.

+3


source to share


2 answers


It totally depends on your original implementation List

. From, ArrayList

you may receive ConcurrentModificationException

(or may receive corrupted data or some other exception: see comment). With the help CopyOnWriteArrayList

you can safely use streaming operations: they will see the state of the list that was actual when the stream was created. In any case, you should check the documentation for the spliterator()

corresponding collection method to make sure it is safe to use in a parallel environment.



+6


source


You should get a concurrent modification exception since you are reading and writing at the same time using this collection.



0


source







All Articles