Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Joiner thread safe?

Is google collections Joiner thread safe?

like image 360
Aravind Yarram Avatar asked Mar 03 '10 20:03

Aravind Yarram


People also ask

Is string join thread safe?

Unlike StringBuffer methods (like append() ) which are synchronized, methods of StringJoiner (like add() ) are not synchronized . Thus it is not thread-safe.

When should I worry about thread safety?

Save this answer. Show activity on this post. Thread safety becomes a concern if there is at least a single entry point which can be accessed by multiple threads. If a piece of code is accessed by multiple threads and is calling other method/class/etc., then all this code tree becomes vulnerable.

Are sets thread safe?

Threadsafe Collections. The collection interfaces in Java – List, Set, Map – have basic implementations that are not threadsafe. The implementations of these that you've been used to using, namely ArrayList, HashMap, and HashSet, cannot be used safely from more than one thread.


2 Answers

Yes! We're not about to repeat the mistakes of SimpleDateFormat. :-)

Joiner needs to receive a similar documentation upgrade to what its sister class Splitter got, which says:

* <p><b>Warning: splitter instances are always immutable</b>; a configuration
* method such as {@code omitEmptyStrings} has no effect on the instance it
* is invoked on! You must store and use the new splitter instance returned by
* the method. This makes splitters thread-safe, and safe to store as {@code
* static final} constants . . .
like image 65
Kevin Bourrillion Avatar answered Sep 19 '22 04:09

Kevin Bourrillion


Its only state is a

  private final String separator;

So yes it's threadsafe.

like image 32
daveb Avatar answered Sep 22 '22 04:09

daveb