Say I have a List
of objects that being modified by only one thread (the thread can add or remove objects) and and another thread occasinally uses stream api from the above collecion to do some stream manipulation of it and at the end (eager operation) return a new collection.
Is this a thread safe scenario? after all only one thread updates the collection.
This totally depends on your source List
implementation. With ArrayList
you may get ConcurrentModificationException
(or may get corrupted data or some other exception: see the comment). With CopyOnWriteArrayList
you can safely use stream operations: they will see the state of the list which was actual when the stream was created. In any case you should check the documentation for spliterator()
method of the corresponding collection to see whether it's safe to use it in concurrent environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With