Use Guava CharMatcher as static fields in a class. Is CharMatcher Technology Safe?

I need to replace some characters in a string: Each DOT for underscore.

Just do: myString.replace(".","_"); which is working.

However, I want to use CharMatcher from Guava, which should have better performance.

 CharMatcher dotCharMatcher = CharMatcher.anyOf("."); dotCharMatcher.replaceFrom(myString, "_"); 

It runs on a server with lots of threads.

Can I make dotCharMatcher static field in a class that uses it, or do I need to create it in every request? (is he safe?)

thanks

+6
source share
1 answer

Yes, checking CharMatcher.java , the instance returned from anyOf is thread safe.

However, the Guava string utilities explained in the documentation say that Joiner and Splitter are thread safe, but do not make the same requirement for CharMatcher .

+6
source

Source: https://habr.com/ru/post/977767/


All Articles