Difference between StringBuilder and StringBuffer

后端 未结 30 2229
独厮守ぢ
独厮守ぢ 2020-11-21 15:06

What is the main difference between StringBuffer and StringBuilder? Is there any performance issues when deciding on any one of these?

相关标签:
30条回答
  • 2020-11-21 15:31

    The javadoc explains the difference:

    This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

    0 讨论(0)
  • 2020-11-21 15:31

    StringBuffer is mutable. It can change in terms of length and content. StringBuffers are thread-safe, meaning that they have synchronized methods to control access so that only one thread can access a StringBuffer object's synchronized code at a time. Thus, StringBuffer objects are generally safe to use in a multi-threaded environment where multiple threads may be trying to access the same StringBuffer object at the same time.

    StringBuilder The StringBuilder class is very similar to StringBuffer, except that its access is not synchronized so that it is not thread-safe. By not being synchronized, the performance of StringBuilder can be better than StringBuffer. Thus, if you are working in a single-threaded environment, using StringBuilder instead of StringBuffer may result in increased performance. This is also true of other situations such as a StringBuilder local variable (ie, a variable within a method) where only one thread will be accessing a StringBuilder object.

    0 讨论(0)
  • 2020-11-21 15:31

    Since StringBuffer is synchronized, it needs some extra effort, hence based on perforamance, its a bit slow than StringBuilder.

    0 讨论(0)
  • 2020-11-21 15:32

    A simple program illustrating the difference between StringBuffer and 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");
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-11-21 15:35

    StringBuffer is synchronized, StringBuilder is not.

    0 讨论(0)
  • 2020-11-21 15:35

    There are no basic differences between StringBuilder and StringBuffer, only a few differences exist between them. In StringBuffer the methods are synchronized. This means that at a time only one thread can operate on them. If there is more than one thread then the second thread will have to wait for the first one to finish and the third one will have to wait for the first and second one to finish and so on. This makes the process very slow and hence the performance in the case of StringBuffer is low.

    On the other hand, StringBuilder is not synchronized. This means that at a time multiple threads can operate on the same StringBuilder object at the same time. This makes the process very fast and hence performance of StringBuilder is high.

    0 讨论(0)
提交回复
热议问题