栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

StringBuilder和StringBuffer的区别

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

StringBuilder和StringBuffer的区别

StringBuilder和StringBuffer的区别

这里从两个角度(性能、线程安全)去分析与区别。

StringBuilder与StringBuffer类结构 StringBuilder类继承结构

StringBuffer类继承结构

StringBuilder方法列表

StringBuffer方法列表

StringBuilder与StringBuffer性能比较

下面通过一个DEMO来对比StringBuilder与StringBuffer的性能,顺带测试String的性能。

package simple.callback.stringbufferbuilder;


public class StringBufferBuilderTest {

    public static void main(String[] args) {
        //int performanceTestLength = 10000000;//1000万
        int performanceTestLength = 10000;
        System.out.println("String性能测试-length-" + performanceTestLength + ":" + appendStringPerformanceTest(performanceTestLength) + "ms");
        System.out.println("String2性能测试-length-" + performanceTestLength + ":" + appendString2PerformanceTest(performanceTestLength) + "ms");
        System.out.println("StringBuffer性能测试-length-" + performanceTestLength + ":" + appendStringBufferPerformanceTest(performanceTestLength) + "ms");
        System.out.println("StringBuilder性能测试-length-" + performanceTestLength + ":" + appendStringBuilderPerformanceTest(performanceTestLength) + "ms");
    }

    
    private static long appendStringPerformanceTest(int length) {
        long start = System.currentTimeMillis();
        String str = "";
        for (int i = 0; i < length; i++) {
            str += "StringBuilder和StringBuffer区别";
        }
        return System.currentTimeMillis() - start;
    }

    
    private static long appendString2PerformanceTest(int length) {
        long start = System.currentTimeMillis();
        String str = new String("");
        for (int i = 0; i < length; i++) {
            str += "StringBuilder和StringBuffer区别";
        }
        return System.currentTimeMillis() - start;
    }

    
    private static long appendStringBufferPerformanceTest(int length) {
        long start = System.currentTimeMillis();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            stringBuffer.append("StringBuilder和StringBuffer区别");
        }
        return System.currentTimeMillis() - start;
    }

    
    private static long appendStringBuilderPerformanceTest(int length) {
        long start = System.currentTimeMillis();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            stringBuilder.append("StringBuilder和StringBuffer区别");
        }
        return System.currentTimeMillis() - start;
    }



}
性能比较输出结果:
String性能测试-length-10000:2324ms
String2性能测试-length-10000:1497ms
StringBuffer性能测试-length-10000:1ms
StringBuilder性能测试-length-10000:0ms

Process finished with exit code 0
String性能测试-length-10000:1967ms
String2性能测试-length-10000:1298ms
StringBuffer性能测试-length-10000:1ms
StringBuilder性能测试-length-10000:0ms

Process finished with exit code 0
String性能测试-length-5000:702ms
String2性能测试-length-5000:400ms
StringBuffer性能测试-length-5000:1ms
StringBuilder性能测试-length-5000:0ms

Process finished with exit code 0

StringBuilder与StringBuffer线程安全分析

从jdk源码上可以看到:

java.lang.StringBuilder#append(java.lang.String)不是线程安全的;

    @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }

java.lang.StringBuffer#append(java.lang.String)是线程安全的;

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

java.lang.AbstractStringBuilder#append(java.lang.String)

    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;
    }

总结
拼接效率很高
StringBuilder
StringBuffer
String
线程安全安全不安全
StringBuilder
StringBuffer
String

记录与总结,2021年 11月 06日 星期六 20:22:42 CST。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/434603.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号