The comparison method, by its nature, is a pure function calculated against objects that can be considered immutable, at least during the sort operation. Mutation of objects during sorting would violate the essential assumption of any sorting algorithm, that is, the order imposed by Comparator is stable throughout the operation.
It follows from the above that, although the comparator must be thread safe when used in parallel, it is usually set without much effort. On the other hand, you need to make sure that it does not contain any unsafe code for one reason or another, for example, using an instance-unsafe instance of a co-author, divided into all calls to compare() .
source share