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