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
.
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.
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.
The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With