Concurrent collections question 74 of 180 mid
What does Collections.unmodifiableList guarantee about thread safety?
- It makes the list thread safe
- Correct: Nothing: it is a read-only view over a list someone else can still mutate, with no happens-before edge for those changes
- It copies the list
- It is the same as
List.copyOf
An unmodifiable wrapper only blocks writes through that reference. The underlying list is unchanged, and if anyone retains a reference and mutates it, readers of the view see those changes with no synchronisation at all: a data race, and possibly a torn view of the internal array.
List.copyOf and List.of are different: they build a genuinely immutable list whose fields are
final, which makes it safe to publish by any means, including a race. That is the final-field
guarantee, and it is the reason immutability is the cheapest thread safety there is.
Quiz yourself on Concurrent collections Suggest a correction