【Java小知识】StringBuilder为什么线程不安全?

前言

我们都知道,String是不可变的,所以在字符串操作比较频繁的时候使用StringBuilder和StringBuffer运行效率更高。 StringBuilder和StringBuffer的区别在于StringBuilder是线程不安全的,而StringBuffer是线程安全的。

为什么呢,今天通过源码来一探究竟…

小试验

写一个小demo,开启10个线程,拼接字符串,并最终输出字符串长度

	public static void main(String[] args) throws InterruptedException {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 1000; j++)
                        s.append("s");
                }
            }).start();
        }
        Thread.sleep(1000);
        System.out.println(s.length());
    }

输出结果:小于10000,同时还抛出异常ArrayIndexOutOfBoundsException,why?

Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at java.lang.String.getChars(String.java:826)
	at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:449)
	at java.lang.StringBuilder.append(StringBuilder.java:136)
	at StringTest$1.run(StringTest.java:10)
	at java.lang.Thread.run(Thread.java:745)
9290

源码分析

我们进入源码来一探究竟,首先StringBuilder和StringBuffer都是继承于AbstractStringBuilder,都是通过一个char数组来存储字符串的。

/**
     * The value is used for character storage.
     */
    char[] value;

    /**
     * The count is the number of characters used.
     */
    int count;

引申:String也是通过char数组来实现的,不同的是String里面的char数组被final修饰,是不可变的。

我们继续看StringBuilder是append()方法,调用的父类AbstractStringBuilder的append()方法。

    @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }
    public AbstractStringBuilder append(String str) {
        if (str == null)
            return appendNull();
        int len = str.length();
        ensureCapacityInternal(count + len);
        str.getChars(0, len, value, count);
        count += len;
        return this;
    }

我们可以发现count+=len不是一个原子操作。假设这个时候的count值是100,len为1,此时两个线程同时拿到了count都是100,执行完加法运算后将结果赋值给count,所以两个线程执行完之后,count值为101,而不是102。这就是为什么测试结果要小于预想的10000了。

那么为什么又会抛出ArrayIndexOutOfBoundsException异常呢?

我们继续看:

ensureCapacityInternal(count + len); //检查原char数组的容量能不能装下新的字符串
str.getChars(0, len, value, count);//将String对象的char数组里面的内容拷贝到StringBuilder对象的char数组里面

判断StringBuilder对象的char数组容量是否足够,不够的话需要进行扩容

    private void ensureCapacityInternal(int minimumCapacity) {
        // overflow-conscious code
        if (minimumCapacity - value.length > 0) {
            value = Arrays.copyOf(value,
                    newCapacity(minimumCapacity));
        }
    }

扩容的逻辑就是new一个新的char数组,新的数组的容量为原来数组的两倍+2。

	private int newCapacity(int minCapacity) {
        // overflow-conscious code
        int newCapacity = (value.length << 1) + 2;
       ....
    }

str.getChars就是将String对象char数组的内容拷贝到StringBuilder对象char数组里面。

	public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
        .....
        System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
    }

所以在拷贝的过程中可能会发生什么呢?

假设现在有两个线程同时执行了append()方法,同时执行到了ensureCapacityInternal()方法,此时count=10。
这个时候线程1的CPU时间片用完了,线程2继续执行。线程2执行完成整个append()方法之后count变成11了。
而线程1继续执行str.getChars()方法的时候拿到的count就是11了,执行char数组拷贝的时候就会抛出ArrayIndexOutOfBoundsException异常了。

StringBuilder为什么线程非安全分析完了,那如果使用StringBuffer呢?运行结果显然就等于10000,那么StringBuffer是如何实现线程安全的呢?

10000

我们继续看StringBuffer的append()方法,同样是调用的父类AbstractStringBuilder的append()方法,但细心的同学可能发现StringBuffer的append()方法的通过关键字的synchronized修饰,所以整个append()操作是线程同步的。

    @Override
    public synchronized StringBuffer append(String str) {
        toStringCache = null;
        super.append(str);
        return this;
    }

关于synchronized关键字我们后续再讲。

总结

所以为什么StringBuilder是线程不安全的:因为StringBuilder的append()方法是不同步。

发布了18 篇原创文章 · 获赞 38 · 访问量 691

猜你喜欢

转载自blog.csdn.net/wudingmei1023/article/details/103835980