package cn.yb.thread;
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadAutomicSlove {
static AtomicInteger auAtomicInteger;
public static void main(String[] args) throws InterruptedException {
int j = 0;
while(j<100) {
auAtomicInteger = new AtomicInteger(0);
Thread thread1 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 1000; i++) {
auAtomicInteger.getAndIncrement();
}
}
});
Thread thread2 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 1000; i++) {
auAtomicInteger.getAndIncrement();
}
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("n的最终值是:"+auAtomicInteger.get());
j++;
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200627174635775.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ2MjY2NTAz,size_16,color_FFFFFF,t_70)