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");
        }

    }
}

其他回答

字符串缓冲区

同步,因此线程安全线程安全,因此速度较慢

字符串拼接

在Java 5.0中引入异步,因此快速高效用户明确需要同步它,如果他想您可以将其替换为StringBuffer而无需任何其他更改

StringBuffer是线程安全的,但StringBuilder不是线程安全的。StringBuilder比StringBuffer更快。StringBuffer已同步,而StringBuilder未同步同步。

StringBuffer和StringBuilder源之间的差异:

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

字符串缓冲区是可变的。它可以在长度和内容方面改变。StringBuffers是线程安全的,这意味着它们有同步的方法来控制访问,这样一次只有一个线程可以访问StringBuffer对象的同步代码。因此,StringBuffer对象在多线程环境中使用通常是安全的,因为多个线程可能同时尝试访问同一StringBuffer。

字符串拼接StringBuilder类与StringBuffer非常相似,只是它的访问不同步,因此不是线程安全的。通过不同步,StringBuilder的性能可以优于StringBuffer。因此,如果您在单线程环境中工作,使用StringBuilder而不是StringBuffer可能会提高性能。这也适用于其他情况,例如StringBuilder局部变量(即方法中的变量),其中只有一个线程将访问StringBuilder对象。