StringBuffer与StringBuilder有什么区别

package String比较;
/*
* StringBuffer与StringBuilder有什么区别
* StringBuilder是JDK5增加的一个新类,功能几乎与StringBuffer相同。区别在于StringBuffer是线程安全的,
* 多个线程同时操作StringBuffer时,能保证同步,而Stringbuilder是线程非安全的。如果只在单线程环境下使用,StringBuilder
* 的效率会更高
*/ public class Test9 { public static void main(String[] args) {
// TODO Auto-generated method stub
String strtemp = "watch_free";
int time = 12000;
@SuppressWarnings("unused")
String str = "";
long start1 = System.currentTimeMillis();
for(int i= 0;i<time;i++){
str+=strtemp;
}
long end1 = System.currentTimeMillis();
System.out.println("String:"+(end1-start1)+"ms");//1218ms StringBuffer sbf = new StringBuffer();
long start2 = System.currentTimeMillis();
for(int i = 0;i<time;i++){
sbf.append(strtemp);
}
long end2 = System.currentTimeMillis();
System.out.println("StringBuffer:"+(end2-start2)+"ms");//2ms StringBuilder sbd = new StringBuilder();
long start3 = System.currentTimeMillis();
for(int i = 0;i<time;i++){
sbd.append(strtemp);
}
long end3 = System.currentTimeMillis();
System.out.println("StringBuilder:"+(end3-start3)+"ms");//1ms
} }
上一篇:移动端的适配坑(一)


下一篇:[移动端]Monkey 日常随笔