Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JAVA StringJoiner Thread-Safe?

Tags:

java

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.

like image 793
rishabh Avatar asked Sep 11 '18 05:09

rishabh


People also ask

What is Java StringJoiner?

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

How do I know if a class is thread-safe?

To test if the combination of two methods, a and b, is thread-safe, call them from two different threads. Put the complete test in a while loop iterating over all thread interleavings with the help from the class AllInterleavings from vmlens. Test if the result is either an after b or b after a.

Which data structures are thread-safe in Java?

The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc.

Is reading thread-safe Java?

Important points about Thread-Safety in Java Since String is immutable in Java, it's inherently thread-safe. 2) Read-only or final variables in Java are also thread-safe in Java.


1 Answers

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:

  • StringJoiner
  • StringBuffer
like image 139
S.K. Avatar answered Oct 31 '22 00:10

S.K.