Guava-libraries - is the Ordering class safe?

guava-libraries has an Ordering class. I wonder if he is safe.

For example, can it be used as a static variable?

public static Ordering<String> BY_LENGTH_ORDERING = new Ordering<String>() { public int compare(String left, String right) { return Ints.compare(left.length(), right.length()); } }; 
+7
java static thread-safety guava
source share
2 answers

It is as thread safe as your compare method.

The default implementation for the order does not contain instance data, so the only important thing is how you define your comparison method.

+8
source share

Yes, Ordering objects are all immutable unless you do something to make them mutable, such as extending Ordering and adding mutable fields or providing mutable Comparator in the from(Comparator) method or mutable Function in onResultOf(Function) .

But as a rule, you really need to get out of the way to create a safe thread.

+5
source share

All Articles