上代码
public class Test {
public static AtomicInteger count = new AtomicInteger(0);
public static class T1 extends Thread {
@Override
public void run() {
for(int index = 0; index < 10000; index++)
{
count.getAndAdd(1);
}
}
public static void main(String[] args) throws Exception{
T1 t1 = new T1();
T1 t2 = new T1();
T1 t3 = new T1();
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
System.out.println(count.get());
}
}
}
这里代码运行后,变量count的值是30000,程序中多线程的同步处理时也没有加锁,就是用到了CAS原理