7

Can anyone tell me if StringJoiner is thread-safe or not?

I know the difference between StringBuilder and StringBuffer but not able to find information about StringJoiner.

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
rishabh
  • 133
  • 9
  • 1
    have you checked the source code? – Scary Wombat Sep 11 '18 at 05:22
  • Looking at the source code, it doesn't seem thread-safe. – ernest_k Sep 11 '18 at 05:23
  • 3
    Have a look at [how to know if java SE class or method is thread safe?](https://stackoverflow.com/questions/32023377/how-to-know-if-java-se-class-or-method-is-thread-safe) – Gunasekar Sep 11 '18 at 05:25
  • 3
    Would you want to use a `StringJoiner` from multiple threads anyway? You'd surely get your strings in an arbitrary order. – Andy Turner Sep 11 '18 at 06:18
  • @AndyTurner, this is very much an issue when used in for example a servlet. One can define the StringJoiner as a `final static` in the hope that it would precompile the joiner logic prior to calling it's toString() method multiple times. This is what we typically do with `java.util.regex.Pattern` instances, which happen to be thread-safe. – YoYo Jan 26 '21 at 22:24
  • @YoYo you do that with a `Pattern` because it is *immutable*, not simply because it's thread-safe: the same `Pattern` can be used multiple times, both by the same thread and by other threads, without changing its state. `StringJoiner` is mutable, and moreover is append-only: if you share one between servlet requests, you will leak data between requests, as well as have it grow indefinitely until you run out of memory. This is a terrible idea. – Andy Turner Jan 27 '21 at 10:20
  • Right, state changes are maintained on a separate `Matcher` instance. But all immutable objects are consequently thread-safe. – YoYo Jan 27 '21 at 15:31

4 Answers4

8

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

Source code from OpenJDK:

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
S.K.
  • 3,597
  • 2
  • 16
  • 31
2

There is zero information in the documentation that would even hint a thread safety property. But, it has a method like StringJoiner::merge that is very often overlooked. This is used to combine two StringJoiners together by two separate threads; and is used internally by the stream API when multiple threads are involved.

So, no, it is not thread safe at all; but can be used to merge two different StringJoiner(s).

Eugene
  • 117,005
  • 15
  • 201
  • 306
0

StringJoiner (in java.util) is different from StringBuilder and StringBuffer (both in java.lang). StringBuilder and StringBuffer act as String containers where you can create a string, append,insert and update the string.

But, as the doc says StringJoiner is to 'construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.'

So, the methods available only support this purpose. Based on the documentation, the class is not thread-safe.

And, the purpose of this class can actually be achieved in a thread-safe manner since this is like a utility class in util package.

Gimhani
  • 1,318
  • 13
  • 23
-4

Joiner instances are always immutable; a configuration method such as useForNull has no effect on the instance it is invoked on! You must store and use the new joiner instance returned by the method. This makes joiners thread-safe, and safe to store as static final constants.

Checkout this link.

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=2ahUKEwimhb-1mrLdAhUZfd4KHbsfCBAQFjABegQICxAE&url=https%3A%2F%2Fgoogle.github.io%2Fguava%2Freleases%2F19.0%2Fapi%2Fdocs%2Fcom%2Fgoogle%2Fcommon%2Fbase%2FJoiner.html&usg=AOvVaw0iHPRevkI6TS31IUFmBkQc

Abhinav
  • 31
  • 6