长安网站建设定制,芜湖先锋网站两学一做,石家庄网络公司名单,北京软件公司名称大全Unsafe类是CAS的核心#xff0c;由于Java方法无法直接访问底层#xff0c;需要通过本地方法(native)来实现#xff0c;Unsafe类相当于一个桥梁。基于Unsafe类#xff0c;可以直接操作特定的内存数据。 我们从上一篇说CAS基本原理的时候#xff0c;有说到一个“资源”被100… Unsafe类是CAS的核心由于Java方法无法直接访问底层需要通过本地方法(native)来实现Unsafe类相当于一个桥梁。基于Unsafe类可以直接操作特定的内存数据。 我们从上一篇说CAS基本原理的时候有说到一个“资源”被100个线程每个线程累加100次最终得到10000该“资源”我们使用AtomicInteger来模拟的。这篇我们就从AtomicInteger更加深入探究CAS。 AtomicInteger的getAndIncrement()方法 /*** Atomically increments by one the current value.** return the previous value*/public final int getAndIncrement() {return unsafe.getAndAddInt(this, valueOffset, 1);} AtomicInteger的getAndIncrement()方法调用的是Unsafe类中的getAndAddInt()方法其中的参数valueOffset是通过Unsafe实例的获取到当前AtomicInteger这个对象的value属性的内存偏移地址。 Unsafe的getAndAddInt()方法
public final int getAndAddInt(Object var1, long var2, int var4) {int var5;do {var5 this.getIntVolatile(var1, var2);} while(!this.compareAndSwapInt(var1, var2, var5, var5 var4));return var5;} 参数var1就是当前需要Unsafe类操作的对象在当前案例中就是AtomicInteger的一个实例。var2则是当前AtomicInteger实例也就是var1的value这个属性的变量的内存地址偏移量。var4就是案例中需要累加的值案例中是1。 接下来是一个 do{ } while循环首先通过Unsafe类的getIntVolatile()方法(native方法)拿到当前操作的AtomicInteger的实例中的value属性的值在使用Unsafe类的compareAndSwapInt()方法(native方法)将当前操作的AtomicInteger的实例中的value属性的原值和新值进行CAS。如果没有交换成功就一直重复上面的动作如果交换成功就跳出当前循环。 AtomicInteger的get()方法 /*** Gets the current value.** return the current value*/public final int get() {return value;} 等100个线程每个都完成100次累加后(案例中使用到了CountDownLatch保证100个线程都操作完成)在调用AtomicInteger的get()方法。我们发现AtomicInteger的get()方法获取的就是value这个值value属性是由volatile修饰的(在多线程环境下保证其可见性)。 最终我们发现AtomicInteger实现多线程并发保证线程安全是通CAS(compare and swap)volatile来实现的从而避免synchronized的高开销提高执行效率。 文章转载自: http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.xtxp.cn.gov.cn.xtxp.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.bpwz.cn.gov.cn.bpwz.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.jqpq.cn.gov.cn.jqpq.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.pmptm.cn.gov.cn.pmptm.cn http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn http://www.morning.jpmcb.cn.gov.cn.jpmcb.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.fqcdh.cn.gov.cn.fqcdh.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.zrgsg.cn.gov.cn.zrgsg.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.rgpbk.cn.gov.cn.rgpbk.cn http://www.morning.zzgkk.cn.gov.cn.zzgkk.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.bkqw.cn.gov.cn.bkqw.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.zsrdp.cn.gov.cn.zsrdp.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.hnhgb.cn.gov.cn.hnhgb.cn http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn http://www.morning.gpxbc.cn.gov.cn.gpxbc.cn http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.qzpsk.cn.gov.cn.qzpsk.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.wfhnz.cn.gov.cn.wfhnz.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.jwskq.cn.gov.cn.jwskq.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.bfrsr.cn.gov.cn.bfrsr.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.czlzn.cn.gov.cn.czlzn.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.hgtr.cn.gov.cn.hgtr.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.rgrdd.cn.gov.cn.rgrdd.cn