在Java中,对字符串的只读访问是否需要同步?
Java 中是否需要同步对字符串的只读访问?
Is it required to synchronize read only access to Strings in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Java 中是否需要同步对字符串的只读访问?
Is it required to synchronize read only access to Strings in Java?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
像
String
这样的不可变对象是线程安全的,因为它们的内部状态永远不会改变,并且final
对这些String
的引用> 对象是线程安全的。但是,如果您有一个对不可变对象的非最终引用,它不是完全线程安全的。
给定:
对<的任何引用code>x 是线程安全的,因为
x
引用的内容永远不会改变,并且String
对象是不可变。并给出:
对
s
的任何引用都不是线程安全的,因为s
可能会更改为其他内容。也就是说:可以改变
s
所指的内容。如果您的引用永远不应该更改,则将它们设为final
,如果预计它们会更改,则应将它们声明为易失性
,以便 JVM 知道重新读取引用的内容,而不是这样做任何可能会错过s
更改的缓存优化。有关线程安全和
final
关键字。Immutable objects like
String
are thread safe, in that their internal state will never change, andfinal
references to thoseString
objects are thread safe.But, if you have a non-final reference to an immutable object, it isn't completely thread safe.
Given :
any references to
x
are thread safe since whatx
refers to can never change and theString
object is immutable.And given :
any references to
s
are not thread safe, becauses
can possibly change to something else. That is :can change what
s
refers to. If your references should never change make themfinal
if they are expected to change you should declare themvolatile
so the JVM knows to re-read the contents of the reference and not do any caching optomizations that might miss changes tos
.See The final word on final for more information on thread safety and the
final
keyword.不,字符串是不可变的。如果您所做的只是读取字符串,那绝对没问题。
编辑添加:是的,如果您有可变属性,则需要同步更改该属性的值。
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.
本身不是针对
String
,而是针对String
的引用,绝对如此。编辑:
不保证所有线程都可以看到未声明为final(或以其他方式同步)的变量的引用。虽然许多线程读取同一个 String 对象的值是安全的,但您需要以某种方式保证所有线程都读取最新的引用。
Not for a
String
, per se, but for the reference to aString
, 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.字符串是不可变的对象,因此本身是线程安全的。请注意,像 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.
每个字符串访问都是只读的(字符串是不可变的),并且由于
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.