There is no such thing as general thread safety or atomicity. Atomic field updates are only atomic in respect to threads accessing the same variable, synchronized
code blocks are executed atomic/thread safe in respect to threads synchronizing on the same instance only.
A stream operation in itself is a purely local operation, this holds even for parallel stream operations, as the threads participating in that operation are unrelated to any other threads. If you use functions with (non local) side effects, which is very discouraged, there are no guarantees, there's no added thread safety nor atomicity for these side effects. The only exception are the terminal operations forEach
and forEachOrdered
, which are intended for producing side effects and well documented regarding the behavior with multiple threads.
So the operation users.stream().map(user->user.getUsername()).collect(Collectors.toList())
, assuming that the method getUsername()
follows the contract and has no side effects, is not visible to any other thread at all. If you publish the returned list to other threads in a thread safe way, it will be safe, if you let it escape in an unsafe way, there will be no guarantees. If you never publish the result to other threads, the question becomes irrelevant.