**** 注意,该测试并不严谨,在赋值操作时测试代码中的方式不同,所以该测试作为定性分析
测试结果为,Synchronized在800ms左右完成了10000次加法,AtomicInteger在700ms左右完成,略胜一筹
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) {
long begin = System.currentTimeMillis();
for(int i=0; i<10000; i++) {
//new sThread().start();
new aThread().start();
}
long end = System.currentTimeMillis();
System.out.println(end-begin);
}
}
class aThread extends Thread{
private static AtomicInteger value = new AtomicInteger();
@Override
public void run() {
value.incrementAndGet();
}
}
class sThread extends Thread{
private static Integer value = 0;
@Override
public void run() {
synchronized (value) {
value ++;
}
}
}