在Java中,对字符串的只读访问是否需要同步?

发布于 2024-11-08 14:39:29 字数 29 浏览 0 评论 0原文

Java 中是否需要同步对字符串的只读访问?

Is it required to synchronize read only access to Strings in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花心好男孩 2024-11-15 14:39:29

String这样的不可变对象是线程安全的,因为它们的内部状态永远不会改变,并且final对这些String的引用> 对象是线程安全的。

但是,如果您有一个对不可变对象的非最终引用,它不是完全线程安全的。

给定:

public final String x = "This reference is thread safe, it can never change";

对<的任何引用code>x 是线程安全的,因为 x 引用的内容永远不会改变,并且 String 对象是不可变

并给出:

public String s = "This reference is not thread safe, it can change";

s 的任何引用都不是线程安全的,因为 s 可能会更改为其他内容。也就是说:

s = "this is another different immutable String";

可以改变s所指的内容。如果您的引用永远不应该更改,则将它们设为final,如果预计它们会更改,则应将它们声明为易失性,以便 JVM 知道重新读取引用的内容,而不是这样做任何可能会错过 s 更改的缓存优化。

public volatile String s = "This reference is expected to change, so tell the JVM";

有关线程安全和final 关键字。

Immutable objects like String are thread safe, in that their internal state will never change, and final references to those String objects are thread safe.

But, if you have a non-final reference to an immutable object, it isn't completely thread safe.

Given :

public final String x = "This reference is thread safe, it can never change";

any references to x are thread safe since what x refers to can never change and the String object is immutable.

And given :

public String s = "This reference is not thread safe, it can change";

any references to s are not thread safe, because s can possibly change to something else. That is :

s = "this is another different immutable String";

can change what s refers to. If your references should never change make them final if they are expected to change you should declare them volatile so the JVM knows to re-read the contents of the reference and not do any caching optomizations that might miss changes to s.

public volatile String s = "This reference is expected to change, so tell the JVM";

See The final word on final for more information on thread safety and the final keyword.

瑶笙 2024-11-15 14:39:29

不,字符串是不可变的。如果您所做的只是读取字符串,那绝对没问题。

编辑添加:是的,如果您有可变属性,则需要同步更改该属性的值。

No. Strings are immutable. If all you're doing is reading the String, you're absolutely fine.

Edit to add: Yes, if you have a mutable property, that changing the value of the property would need to be synchronized.

如梦 2024-11-15 14:39:29

本身不是针对String,而是针对String 的引用,绝对如此。

编辑:

不保证所有线程都可以看到未声明为final(或以其他方式同步)的变量的引用。虽然许多线程读取同一个 String 对象的值是安全的,但您需要以某种方式保证所有线程都读取最新的引用。

Not for a String, per se, but for the reference to a String, definitely.

Edit:

The references of variables that are not declared as final (or otherwise synchronized) are not guaranteed to be seen by all threads. While it is safe for many threads to read the value of the same String object, you need to somehow guarantee that all threads are reading the most up to date reference.

情定在深秋 2024-11-15 14:39:29

字符串是不可变的对象,因此本身是线程安全的。请注意,像 String.toUpperCase() 这样的函数不会更改 String 对象本身,而是返回一个新的 String。

StringBuffer 和 StringBuilder 是可变对象。 StringBuffer 是线程安全的,而 StringBuilder 不是。

Strings are immutable objects and therefore thread-safe in themselves. Note that a function like String.toUpperCase() does not change the String object itself but returns a new String.

StringBuffer and StringBuilder are mutable objects. StringBuffer is thread-safe and StringBuilder is not.

那请放手 2024-11-15 14:39:29

每个字符串访问都是只读的(字符串是不可变的),并且由于 String 实例的内容无法更改,因此无需同步。

Every String access is read only (Strings are immutable) and because the content of a String instance can't change, there's no need to synchronize.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文