解决同样的问题的更高效的方法,使用AtomXXX类 AtomXXX类本身方法都是原子性的,但不能保证多个方法连续调用是原子性的
public class T {
/*volatile*/ //int count = 0;
AtomicInteger count = new AtomicInteger(0);
/*synchronized*/ void m() {
for (int i = 0; i < 10000; i++)
//if count.get() < 1000 //注意:加上这句话和下面一起就不构成原子性了
count.incrementAndGet(); //它是具备原子性的,用来替代count++
}
public static void main(String[] args) {
T t = new T();
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
threads.add(new Thread(t::m, "thread-" + i));
}
threads.forEach((o) -> o.start());
threads.forEach((o) -> {
try {
o.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println(t.count);
}
}
结果:
还有一个小问题:请看视频的第79分钟。