ConcurrentHashMap
ConcurrentHashMap相比于Hashtable在线程安全的基础上提供了更好的写并发能力,但同时降低了对读一致性的要求。
ConcurrentLinkedHashMap
atomicinteger
---源码
public final long getAndIncrement() {
while (true) {
long current = get();
long next = current + 1;
//当+1操作成功的时候直接返回,退出此循环
if (compareAndSet(current, next))
return current;
}
}
//调用JNI实现CAS
public final boolean compareAndSet(long expect, long update) {
return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
}