packagecom.jason.java.duoxiancheng.volatilekw;importjava.util.concurrent.atomic.AtomicInteger;/**
* 使用原子类进行自增
* @author Jason
*/publicclassTest03{publicstaticvoidmain(String[] args)throwsInterruptedException{for(int i =0; i <10; i++){newMyThread().start();}Thread.sleep(100);System.out.println(MyThread.count.get());}staticclassMyThreadextendsThread{//使用 AtomicInteger 对象privatestaticAtomicInteger count =newAtomicInteger();publicstaticvoidaddCount(){for(int i =0; i <1000; i++){
count.getAndIncrement();}System.out.println(Thread.currentThread().getName()+" count="+ count.get());}@Overridepublicvoidrun(){addCount();}}}