StringBuffer源码浅析(大致结构和append方法)

StringBuffer类继承自AbstractStringBuilder抽象类,实现Serializable序列化接口和CharSequence接口。

AbstractStringBuilder抽象类实现Appendabel,CharSequence接口。

另外,StringBuilder的源码跟StringBuffer类似,看完Buffer再看Builder,找出不同点看看就大致可以。

 

一,关于append方法浅析。

append方法都做了同步声明,其中源码如下

 

public synchronized StringBuffer append(String str) {
	super.append(str);
        return this;
}

 

1、StringBuffer的方法都加了synchronized关键字,线程安全。

2、synchronized代表方法加锁,例如线程A,要运行到声明了synchronized 方法时,先检查有没有其他线程B,C正在使用这synchronized 方法,若有就先等B,C线程运行完这个synchronized 方法后,在运行A,若无直接运行,

3、synchronized两种用法:synchronized方法和synchronized块。

synchronized(syncObject) {  // synchronized代码块
  //允许访问控制的代码
}

4、围观append源码

     第一步:StringBuffer类体内

 

/*

 *  可见是通过super调用父类AbstractStringBuilder中定义的append方法。
 */
public synchronized StringBuffer append(StringBuffer sb) {
        super.append(sb);
        return this;
}

 

 

    第二步:AbstractStringBuilder抽象类体内

 

/**
 *  AbstractStringBuilder属性有:
 *          char value[];  // The value is used for character storage(). 容量
 *          int count;  // The count is the number of characters used.实际字符数
 */
public AbstractStringBuilder append(StringBuffer sb) {
	if (sb == null)
            return append("null");
	int len = sb.length();
	int newCount = count + len;  
	if (newCount > value.length)
	    expandCapacity(newCount); // 若value存储容量不够需扩容。扩容方法省略暂不分析,基本上根据Arrays.copyOf()方法,复制指定的数组,以使副本具有指定的长度。到头来copyOf的源码一样是利用arraycopy方法来复制数组和扩容
	sb.getChars(0, len, value, count); // 把sb的内容0->len复制到value中,注意value参数,此时的value是已经扩容后的value。
	count = newCount;  // 更新新count值
	return this;
}

    原有count要加上len(len也就是新增的count)成newCount,若这newCount总数要大于原有value的容量,就要     扩容才可以容纳newCount个字符。再用getChars复制数组。

 

    第三步:AbstractStringBuilder里定义的getChars方法体,是对System.arraycopy方法 + 边界检查的进一步封装                 而已。

 

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) //  dstBegin就是原有count接下来的位置
    {
	if (srcBegin < 0)
	    throw new StringIndexOutOfBoundsException(srcBegin);
	if ((srcEnd < 0) || (srcEnd > count))
	    throw new StringIndexOutOfBoundsException(srcEnd);
        if (srcBegin > srcEnd)
            throw new StringIndexOutOfBoundsException("srcBegin > srcEnd"); //以上是Bound Check
	System.arraycopy(value, srcBegin,dst, dstBegin, srcEnd - srcBegin);  // 从指定源数组中复制一个数组
}

    arraycopy方法参数

          src - 源数组。

srcPos - 源数组中的起始位置。 dest - 目标数组。 destPos - 目标数据中的起始位置。 length - 要复制的数组元素的数量。 

    注意value和dst

这个value是sb.value,srcEnd-srcBegin个元素全部复制dst(也就是扩容的value)中。(这里我理解正确吗?)

 

待续...

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值