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。

 

 

总结:

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值