简体   繁体   English

JAVA StringJoiner线程安全吗?

[英]Is JAVA StringJoiner Thread-Safe?

Can anyone tell me if StringJoiner is thread-safe or not? 任何人都可以告诉我StringJoiner是否是线程安全的?

I know the difference between StringBuilder and StringBuffer but not able to find information about StringJoiner . 我知道StringBuilderStringBuffer之间的区别,但无法找到有关StringJoiner信息。

Unlike StringBuffer methods (like append() ) which are synchronized, methods of StringJoiner (like add() ) are not synchronized . 与同步的StringBuffer方法(如append() )不同, StringJoiner方法(如add() )不synchronized Thus it is not thread-safe . 因此它不是 线程安全的

Source code from OpenJDK : OpenJDK的源代码:

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. 但是,它有一个像StringJoiner::merge这样的方法,经常被忽视。 This is used to combine two StringJoiners together by two separate threads; 这用于通过两个单独的线程combine两个StringJoiner组合在一起; and is used internally by the stream API when multiple threads are involved. 并且当涉及多个线程时由流API在内部使用。

So, no, it is not thread safe at all; 所以,不,它根本不是线程安全的; but can be used to merge two different StringJoiner (s). 可用于合并两个不同的StringJoiner

StringJoiner (in java.util ) is different from StringBuilder and StringBuffer (both in java.lang ). StringJoiner (在java.util )与StringBuilderStringBuffer (都在java.lang )不同。 StringBuilder and StringBuffer act as String containers where you can create a string, append,insert and update the string. StringBuilderStringBuffer充当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.' 但是,正如文档所说, StringJoiner“构造一个由分隔符分隔的字符序列,并且可选地以提供的前缀开头,并以提供的后缀结尾。”

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. 而且,这个类的目的实际上可以以线程安全的方式实现,因为它就像util包中的实用程序类。

Joiner instances are always immutable; Joiner实例总是不可变的; a configuration method such as useForNull has no effect on the instance it is invoked on! 一个配置方法,如useForNull对它被调用的实例没有影响! You must store and use the new joiner instance returned by the method. 您必须存储并使用该方法返回的新joiner实例。 This makes joiners thread-safe, and safe to store as static final constants. 这使得joiners成为线程安全的,并且可以安全地存储为静态最终常量。

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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM