1. 程式人生 > >JDK8原始碼閱讀筆記--------java.lang.StringBuffer

JDK8原始碼閱讀筆記--------java.lang.StringBuffer

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

意思是StringBuffer是執行緒安全的,可變的字元序列。類似於String,但是可以修改。字元緩衝區(String buffer)對於多執行緒是安全的,這些方法在必要時是同步的,這樣任何特定例項上的所有操作就會按照某種序列順序進行,這與所涉及的每個單獨執行緒呼叫方法的順序是一致的。

為什麼執行緒安全?因為所有方法都加了synchronized關鍵詞。

測試StringBuffer和StringBuilder執行緒安全:

 

package com.whl;

/**
 * Author heling on 2018/11/22
 */
public class Test implements Runnable {

    private StringBuffer sb1;

    private StringBuilder sb2;

    public Test(StringBuffer sb1,StringBuilder sb2) {
        this.sb1 = sb1;
        this.sb2= sb2;
    }

    @Override
    public void run() {

        for (int i = 0; i < 1000; i++) {
            sb1.append("a");
            sb2.append("b");
        }
        System.out.println(sb1.length() + ":" + sb2.length());
    }

    public static void main(String[] args) {
        StringBuffer sb1 = new StringBuffer();
        StringBuilder sb2 = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            new Thread(new Test(sb1,sb2)).start();
        }

    }
}

結果:

總長度應該為100*1000=100000,可見StringBuffer安全,StringBuilder不安全。

 繼承AbstractStringBuilder

1.無(有)參構造

StringBuffer stringBuffer = new StringBuffer();
預設是一個初始容量為16的字元空陣列。可以指定初始容量:StringBuffer stringBuffer = new StringBuffer(100);

2.StringBuffer sb= new StringBuffer(String str);

容量為16+str.length()

3.length()

Returns the length of this character sequence.

4.capacity()

Returns the current capacity. 

5.ensureCapacity(int minimumCapacity)

Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
1.The minimumCapacity argument.
2.Twice the old capacity, plus 2.

意思是如果當前容量不大於minimumCapacity,則容量不變;否則擴容:Capacity*2 + 2;

 public static void main(String[] args) {
        //1.原容量不大於minimumCapacity
        String str = "whl";
        StringBuffer sb = new StringBuffer(str);
        System.out.println(sb.capacity());//19
        sb.ensureCapacity(19);
        System.out.println(sb.capacity());//19

        //2.原容量小於minimumCapacity
        String str2 = "whl";
        StringBuffer sb2 = new StringBuffer(str);
        System.out.println(sb2.capacity());//19
        sb2.ensureCapacity(20);
        System.out.println(sb2.capacity());//40=19*2+2
    }

6.append(String str),append(StringBuffer sb)系列方法

Appends the specified string to this character sequence.

此過程可能會呼叫expandCapacity方法(擴容)。

7.delete系列方法

Removes the characters in a substring of this sequence.

8.insert系列方法

9.reverse()

Causes this character sequence to be replaced by the reverse of the sequence.

10.toString()

轉化為String。

 

 

總結: