怎么查一个网站的备案信息,宁波关键词网站排名,内网 做 网站,安卓软件商店专栏系列文章地址#xff1a;https://blog.csdn.net/qq_26437925/article/details/145290162
本文目标#xff1a;
理解ConcurrentHashMap为什么线程安全#xff1b;ConcurrentHashMap的具体细节还需要进一步研究 目录 ConcurrentHashMap介绍JDK7的分段锁实现JDK8的synchr…专栏系列文章地址https://blog.csdn.net/qq_26437925/article/details/145290162
本文目标
理解ConcurrentHashMap为什么线程安全ConcurrentHashMap的具体细节还需要进一步研究 目录 ConcurrentHashMap介绍JDK7的分段锁实现JDK8的synchronized CAS实现put方法 ConcurrentHashMap介绍
百度AI介绍如下
JDK7的分段锁实现
在 JDK7 中ConcurrentHashMap 使用“分段锁”机制实现线程安全数据结构可以看成是”Segment数组HashEntry数组链表”一个 ConcurrentHashMap 实例中包含若干个 Segment 实例组成的数组每个 Segment 实例又包含由若干个桶每个桶中都是由若干个 HashEntry 对象链接起来的链表。
Segment 类继承 ReentrantLock 类锁的粒度为其中一个Segment而不是整体。 JDK8的synchronized CAS实现 每个桶可能是链表结构或者红黑树结构锁针对桶的头节点加锁粒度小
put方法
定位Node数组位置使用CAS操作定位真正进行插入操作的时候会使用synchronized关键字加锁头部
/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent) {// key, value 都不能为nullif (key null || value null) throw new NullPointerException();int hash spread(key.hashCode());int binCount 0;for (NodeK,V[] tab table;;) {NodeK,V f; int n, i, fh;// 如果Node数组是空则进行初始化初始化是CAS操作if (tab null || (n tab.length) 0)tab initTable();else if ((f tabAt(tab, i (n - 1) hash)) null) {// 数组位置节点为null则CAS方式进行添加Node到数组位置if (casTabAt(tab, i, null,new NodeK,V(hash, key, value, null)))break; // no lock when adding to empty bin}else if ((fh f.hash) MOVED)// 如果数组位置节点正在迁移则帮助迁移tab helpTransfer(tab, f);else {// 没有迁移且数组位置不是空则进行聊表或者红黑树的插入操作可能涉及到链表转红黑树V oldVal null;// 直接用 synchronized 锁住 链表或者红黑树的头部synchronized (f) {if (tabAt(tab, i) f) {// 链表遍历判断替换老值或者进行尾插if (fh 0) {binCount 1;for (NodeK,V e f;; binCount) {K ek;if (e.hash hash ((ek e.key) key ||(ek ! null key.equals(ek)))) {oldVal e.val;if (!onlyIfAbsent)e.val value;break;}NodeK,V pred e;if ((e e.next) null) {pred.next new NodeK,V(hash, key,value, null);break;}}}// 红黑树替换老值或者进行红黑树插入else if (f instanceof TreeBin) {NodeK,V p;binCount 2;if ((p ((TreeBinK,V)f).putTreeVal(hash, key,value)) ! null) {oldVal p.val;if (!onlyIfAbsent)p.val value;}}}}if (binCount ! 0) {if (binCount TREEIFY_THRESHOLD)treeifyBin(tab, i);if (oldVal ! null)return oldVal;break;}}}addCount(1L, binCount);return null;
}put完成后addCount(1L, binCount);会进行数量统计和扩容判断操作也是CAS操作
/*** Adds to count, and if table is too small and not already* resizing, initiates transfer. If already resizing, helps* perform transfer if work is available. Rechecks occupancy* after a transfer to see if another resize is already needed* because resizings are lagging additions.** param x the count to add* param check if 0, dont check resize, if 1 only check if uncontended*/
private final void addCount(long x, int check) {CounterCell[] as; long b, s;if ((as counterCells) ! null ||!U.compareAndSwapLong(this, BASECOUNT, b baseCount, s b x)) {CounterCell a; long v; int m;boolean uncontended true;if (as null || (m as.length - 1) 0 ||(a as[ThreadLocalRandom.getProbe() m]) null ||!(uncontended U.compareAndSwapLong(a, CELLVALUE, v a.value, v x))) {fullAddCount(x, uncontended);return;}if (check 1)return;s sumCount();}if (check 0) {NodeK,V[] tab, nt; int n, sc;while (s (long)(sc sizeCtl) (tab table) ! null (n tab.length) MAXIMUM_CAPACITY) {int rs resizeStamp(n);if (sc 0) {if ((sc RESIZE_STAMP_SHIFT) ! rs || sc rs 1 ||sc rs MAX_RESIZERS || (nt nextTable) null ||transferIndex 0)break;if (U.compareAndSwapInt(this, SIZECTL, sc, sc 1))transfer(tab, nt);}else if (U.compareAndSwapInt(this, SIZECTL, sc,(rs RESIZE_STAMP_SHIFT) 2))transfer(tab, null);s sumCount();}}
}
文章转载自: http://www.morning.sjbty.cn.gov.cn.sjbty.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.pwppk.cn.gov.cn.pwppk.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.bwttp.cn.gov.cn.bwttp.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.fqnql.cn.gov.cn.fqnql.cn http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.wdhhz.cn.gov.cn.wdhhz.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.yrgb.cn.gov.cn.yrgb.cn http://www.morning.lyldhg.cn.gov.cn.lyldhg.cn http://www.morning.kqhlm.cn.gov.cn.kqhlm.cn http://www.morning.bfrsr.cn.gov.cn.bfrsr.cn http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn http://www.morning.gmplp.cn.gov.cn.gmplp.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.nuobeiergw.cn.gov.cn.nuobeiergw.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.njqpg.cn.gov.cn.njqpg.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.wgtnz.cn.gov.cn.wgtnz.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.kmqwp.cn.gov.cn.kmqwp.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.wnbpm.cn.gov.cn.wnbpm.cn http://www.morning.yrnyz.cn.gov.cn.yrnyz.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.cndxl.cn.gov.cn.cndxl.cn http://www.morning.mxnhq.cn.gov.cn.mxnhq.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.zqkr.cn.gov.cn.zqkr.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn