51,411
社区成员
发帖
与我相关
我的任务
分享
public class Volatile {
volatile static int i = 0;
static int threadNum = 100;
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[threadNum];
for (int j = 0; j < threadNum; j++){
threads[j] = new Thread(new Runnable() {
@Override
public void run() {
for (int a = 0; a < 100; a++){
i+=1;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
threads[j].start();
}
for (int j = 0; j < threadNum; j++){
threads[j].join();
}
System.out.println(i);
}
}

