StringBuffer和StringBuilder之间的主要区别是什么?在决定其中任何一项时是否存在性能问题?


当前回答

一个简单的程序说明了StringBuffer和StringBuilder之间的区别:

/**
 * Run this program a couple of times. We see that the StringBuilder does not
 * give us reliable results because its methods are not thread-safe as compared
 * to StringBuffer.
 * 
 * For example, the single append in StringBuffer is thread-safe, i.e.
 * only one thread can call append() at any time and would finish writing
 * back to memory one at a time. In contrast, the append() in the StringBuilder 
 * class can be called concurrently by many threads, so the final size of the 
 * StringBuilder is sometimes less than expected.
 * 
 */
public class StringBufferVSStringBuilder {

    public static void main(String[] args) throws InterruptedException {

        int n = 10; 

        //*************************String Builder Test*******************************//
        StringBuilder sb = new StringBuilder();
        StringBuilderTest[] builderThreads = new StringBuilderTest[n];
        for (int i = 0; i < n; i++) {
            builderThreads[i] = new StringBuilderTest(sb);
        }
        for (int i = 0; i < n; i++) {
            builderThreads[i].start();
        }
        for (int i = 0; i < n; i++) {
            builderThreads[i].join();
        }
        System.out.println("StringBuilderTest: Expected result is 1000; got " + sb.length());

        //*************************String Buffer Test*******************************//

        StringBuffer sb2 = new StringBuffer();
        StringBufferTest[] bufferThreads = new StringBufferTest[n];
        for (int i = 0; i < n; i++) {
            bufferThreads[i] = new StringBufferTest(sb2);
        }
        for (int i = 0; i < n; i++) {
            bufferThreads[i].start();
        }
        for (int i = 0; i < n; i++) {
            bufferThreads[i].join();
        }
        System.out.println("StringBufferTest: Expected result is 1000; got " + sb2.length());

    }

}

// Every run would attempt to append 100 "A"s to the StringBuilder.
class StringBuilderTest extends Thread {

    StringBuilder sb;

    public StringBuilderTest (StringBuilder sb) {
        this.sb = sb;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            sb.append("A");
        }

    }
}


//Every run would attempt to append 100 "A"s to the StringBuffer.
class StringBufferTest extends Thread {

    StringBuffer sb2;

    public StringBufferTest (StringBuffer sb2) {
        this.sb2 = sb2;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            sb2.append("A");
        }

    }
}

其他回答

StringBuffer已同步,StringBuilder未同步。

其他人正确地指出了两者之间的关键区别。然而,在性能方面,我想补充一点,JVM级别的优化“锁定Elision”可以使同步上下文中的性能差异几乎不存在。关于这一点的精彩阅读在这里和这里

但需要借助一个例子来明确区别吗?StringBuffer或StringBuilder

只需使用StringBuilder,除非您确实试图在线程之间共享缓冲区。StringBuilder是原始同步StringBuffer类的非同步(开销更少=效率更高)弟弟。

StringBuffer排名第一。Sun关注所有条件下的正确性,因此他们使其同步,以使其线程安全,以防万一。

StringBuilder后来出现了。StringBuffer的大多数使用都是单线程的,不必要地支付了同步的成本。

由于StringBuilder是没有同步的StringBuffer的替代品,因此任何示例之间都不会有差异。

如果您试图在线程之间共享,可以使用StringBuffer,但要考虑是否需要更高级别的同步,例如,如果您同步使用StringBuilder的方法,可能不需要使用StringBuffer。

很好的问题

以下是我注意到的差异:

字符串缓冲区:-

StringBuffer is  synchronized
StringBuffer is  thread-safe
StringBuffer is  slow (try to write a sample program and execute it, it will take more time than StringBuilder)

StringBuilder:-

 StringBuilder is not synchronized 
 StringBuilder is not thread-safe
 StringBuilder performance is better than StringBuffer.

常见问题:-

两者都有相同的方法和相同的签名。两者都是可变的。

基本上,StringBuffer方法是同步的,而StringBuilder不是。

这些操作“几乎”相同,但在单个线程中使用同步方法是过度的。

这差不多了。

StringBuilder API报价:

此类[StringBuilder]提供了与StringBuffer兼容的API,但不保证同步。该类被设计为在单个线程使用字符串缓冲区的地方(通常情况下)作为StringBuffer的替代品。在可能的情况下,建议优先使用该类而不是StringBuffer,因为在大多数实现中,它会更快。

所以它是用来替代它的。

Vector和ArrayList也是如此。