标签:imp incr join() ted row 操作 ola led exception
import java.util.concurrent.atomic.AtomicInteger;
/**
* 原子操作的类 atomic
*/
public class VolatileDemo {
static AtomicInteger i = new AtomicInteger(0);
public static class PlusTask implements Runnable {
@Override
public void run() {
// synchronized (VolatileDemo.class){
for (int j = 0; j < 10000; j++) {
i.incrementAndGet(); //自增
}
// }
}
}
public static void main(String[] args) throws InterruptedException{
Thread[] threads = new Thread[10];
for (int a = 0; a < 10; a++) {
threads[a] = new Thread(new PlusTask());
threads[a].start();
}
for (int a = 0; a < 10; a++) {
threads[a].join();
}
System.out.println(i.get());//i的值小于10000
}
}
标签:imp incr join() ted row 操作 ola led exception
原文地址:https://www.cnblogs.com/fly-book/p/11442613.html