publicfinalint incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
<p class="indent">
比较下来结果,前者花费时间是:0.713189740 seconds time elapsed 后者花费时间是:3.271893871 seconds time elapsed 性能相差很多,有锁性能比较慢,关键原因是CPU的二级缓存需要同步。