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);
}
本文深入探讨了ConcurrentHashMap相较于Hashtable的优势,特别是在写并发能力和读一致性之间的权衡。同时,详细解析了AtomicInteger的源码,包括getAndIncrement()方法如何利用compareAndSet()实现线程安全的原子操作。
797

被折叠的 条评论
为什么被折叠?



