具体可以javap看看两段代码的异同就知道了
public class StringBufferWithoutSync {
public void add(String str1, String str2) {
//StringBuffer是线程安全,由于sb只会在append方法中使用,不可能被其他线程引用
//因此sb属于不可能共享的资源,JVM会自动消除内部的锁
StringBuffer sb = new StringBuffer();
sb.append(str1).append(str2);
}
public static void main(String[] args) {
StringBufferWithoutSync withoutSync = new StringBufferWithoutSync();
for (int i = 0; i < 1000; i++) {
withoutSync.add("aaa", "bbb");
}
}
}
前者不加锁
public class CoarseSync {
public static String copyString100Times(String target){
int i = 0;
StringBuffer sb = new StringBuffer();
while (i<100){
sb.append(target);
}
return sb.toString();
}
}
后者会只加一次锁