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不安全。

JDK8源码阅读笔记--------java.lang.StringBuffer_第1张图片

 继承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。

 

 

总结:

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(JDK)