简体   繁体   English

当他们说StringBuffer类已同步时,这到底是什么意思? 是否已声明该类的所有方法同步?

[英]What exactly does it mean when they say that StringBuffer class is synchronized? Are all methods of that class declared synchronized?

I know that StringBuffer class is synchronized(or thread-safe) while StringBuilder is not. 我知道StringBuffer类是同步的(或线程安全的),而StringBuilder不是。 Does this mean that all the methods within StringBuffer such as append are declared with the keyword synchronized ? 这是否意味着StringBuffer中的所有方法(例如append)都用关键字sync声明了?

I checked the hava API: http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html 我检查了hava API: http ://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html

As per the API, methods such as public StringBuffer append(String str) are not declared with the keyword synchronized. 根据API,未使用关键字public StringBuffer append(String str)声明public StringBuffer append(String str)方法。

Can anyone explain? 谁能解释?

The synchronized keyword doesn't show up in the generated JavaDoc, but if you open the source code of StringBuffer , you'll see that each public method which can change the state of the instance actually has synchronized keyword in its signature, eg. synchronized关键字未显示在生成的JavaDoc中,但是,如果打开StringBuffer的源代码,则会看到每个可以更改实例状态的公共方法实际上在其签名中都带有synchronized关键字。

public synchronized StringBuffer append(StringBuffer sb) {
    toStringCache = null;
    super.append(sb);
    return this;
}

As others mentioned in the comments static doesn't directly relate to thread safety. 正如评论中其他人提到的,static与线程安全性没有直接关系。 If you meant that the methods are not declared with synchronized keyword, then this should answer your question. 如果您的意思是方法未使用synchronized关键字声明,那么这应该可以回答您的问题。

"Thread safety" is a slippery idea. “线程安全”是一个棘手的想法。

When somebody says that a class such as StringBuffer is "thread safe" they usually mean that an instance of the class will not behave in weird, unexpected ways when it is accessed by more than one thread. 当有人说诸如StringBuffer之类的类是“线程安全的”时,它们通常意味着该类的实例在被多个线程访问时不会以奇怪的,不可预料的方式表现。 For example, if thread A tries to append "yellow" to a StringBuffer at the same time as thread B appends "green", the final value might be "yellowgreen" or it might be "greenyellow", but it won't be "yegrelleown". 例如,如果线程A尝试在线程B附加“ green”的同时将“ yellow”附加到StringBuffer,则最终值可能是“ yellowgreen”,也可能是“ greenyellow”,但不会是“ yegrelleown”。 No IllegalStateException will be thrown, and it won't crash the JVM. 不会抛出IllegalStateException,并且不会使JVM崩溃。

Those guarantees make each individual instance of the class thread safe, but they won't make your program thread safe. 这些保证使类线程的每个实例安全,但不会使程序线程安全。 If your program has some thread C that tries to read the StringBuffer at the same time, then it might get either of the two values above, or it might get just "yellow", or just "green", or even the empty string. 如果您的程序中有一些线程C试图同时读取StringBuffer,则它可能会获得上述两个值之一,或者可能只是“黄色”,“绿色”,甚至是空字符串。 If it's important for thread C to see both color names in the string, then you will need additional synchronization to guarantee that it happens that way. 如果对于线程C而言,在字符串中同时看到两个颜色名称很重要,那么您将需要额外的同步以确保这种方式发生。

Building a program entirely out of "thread-safe" classes does not automatically make the whole program "thread safe." 完全在“线程安全”类之外构建程序不会自动使整个程序成为“线程安全”。

As other answers have already told you, the StringBuffer class achieves thread-safety by making all of its methods synchronized , but that is not the only way to achieve thread-safety, and in some cases, that may not be enough to achieve it. 正如其他答案已经告诉您的那样,StringBuffer类通过使其所有方法都synchronized来实现线程安全,但这不是实现线程安全的唯一方法,在某些情况下,可能还不足以实现这一点。 If you want to know what is thread-safe and what isn't, it's important to read the documentation. 如果您想知道什么是线程安全的,什么不是,请务必阅读文档。

Also, when you read it, pay attention to how the class was meant to be used. 另外,在阅读它时,请注意该类的用途。 If somebody advertizes a class as "thread safe" in some particular use-case, that doesn't mean it will be "thread safe" in other use cases 如果有人在某个特定用例中将某个类广告宣传为“线程安全”,那并不意味着在其他用例中它将是“线程安全”的

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

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