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)中。(这里我理解正确吗?)
待续...