AtomicInteger 线程安全的原子操作类 ConcurrentHashMap线程安全的hashMap LinkedHashMap保证顺序输出的Map

AtomicInteger count = new AtomicInteger(0)  

count.incrementAndGet();

 

/**
* Atomically increments by one the current value.
*
* @return the updated value
*/
public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}

其实是CAS指令(Compare-And-Swap比较并交换)

posted on 2020-01-19 21:24  潮流教父孙笑川  阅读(64)  评论(0)    收藏  举报

导航