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


当前回答

StringBuilder和StringBuffer之间没有基本区别,它们之间只有一些区别。在StringBuffer中,方法是同步的。这意味着一次只能有一个线程对它们进行操作。如果有多个线程,那么第二个线程将不得不等待第一个线程完成,第三个线程将必须等待第一个和第二个完成,依此类推。这使得进程非常缓慢,因此StringBuffer的性能很低。

另一方面,StringBuilder不同步。这意味着多个线程可以同时对同一StringBuilder对象进行操作。这使得过程非常快,因此StringBuilder的性能很高。

其他回答

字符串缓冲区:

多线程已同步比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");
        }

    }
}

StringBuilder是在Java1.5中引入的,因此它不能与早期的JVM一起使用。

从Javadocs:

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

StringBuilder(在Java5中引入)与StringBuffer相同,只是其方法不同步。这意味着它比后者具有更好的性能,但缺点是它不是线程安全的。

阅读教程了解更多详细信息。

StringBuffer已同步,StringBuilder未同步。