Unsafe是Java无锁操做的基石,在无锁并发类中都少不了它们的身影,好比ConcurrentHashMap, ConcurrentLinkedQueue, 都是由Unsafe类来实现的。相对于与Java中的锁,它基本无开销,会原地等待。本文主要介绍下Unsafe中的主要操做。segmentfault
/** * 比较obj的offset处内存位置中的值和指望的值,若是相同则更新。此更新是不可中断的。 * * @param obj 须要更新的对象 * @param offset obj中整型field的偏移量 * @param expect 但愿field中存在的值 * @param update 若是指望值expect与field的当前值相同,设置filed的值为这个新值 * @return 若是field的值被更改返回true */ public native boolean compareAndSwapInt(Object obj, long offset, int expect, int update);
这个就是著名的CAS操做了,分为三步来作数组
CAS家族还包括有,compareAndSwapObject(), compareAndSwapLong(), compareAndSwapInt()等等并发
用AtomicInteger中一个经典的例子来讲明:this
public final int getAndAdd(int delta) { return unsafe.getAndAddInt(this, valueOffset, delta); } //unsafe.getAndAddInt public final int getAndAddInt(Object var1, long var2, int var4) { int var5; do { /**获取原始值*/ var5 = this.getIntVolatile(var1, var2); /**确认原始值没有被其它线程修改时,再执行更新var5+var4操做,若是 被其它线程修改过了,则会原地等待,持续循环*/ } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4)); return var5; }
这里能够看出CAS操做存在的问题,在低竞态状况下是ok的,可是在高竞态状况下,while循环会一直消耗cpu资源线程
/*** * Sets the value of the integer field at the specified offset in the * supplied object to the given value. This is an ordered or lazy * version of <code>putIntVolatile(Object,long,int)</code>, which * doesn't guarantee the immediate visibility of the change to other * threads. It is only really useful where the integer field is * <code>volatile</code>, and is thus expected to change unexpectedly. * * @param obj the object containing the field to modify. * @param offset the offset of the integer field within <code>obj</code>. * @param value the new value of the field. * @see #putIntVolatile(Object,long,int) */ public native void putOrderedInt(Object obj, long offset, int value);
将obj对象的偏移量为offset的位置修改成value,由于Java中没有内存操做,而Unsafe的这个操做正好补充了内存操做的不足。也能够用于数组操做,好比ConcurrentHashMap中就大量用到了该操做rest
Segment<K,V> s0 = new Segment<K,V>(loadFactor, (int)(cap * loadFactor), (HashEntry<K,V>[])new HashEntry[cap]); Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize]; // 往数组下标为0的位置,写入s0: ss[0]=s0 UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]
须要注意的是obj须要设置为Volatile,不然对于其它线程会不可见code
该操做会插入一个#storestore的内存屏障,而非putXxxVolatile的#storeload的内存屏障,因此效率会高。所谓#storestore的内存屏障,是之虚拟机在对于这样的语句Store1; StoreStore; Store2,在Store2及后续写入操做执行前,保证Store1的写入操做对其它处理器可见。对象
/*** * Sets the value of the integer field at the specified offset in the * supplied object to the given value, with volatile store semantics. * * @param obj the object containing the field to modify. * @param offset the offset of the integer field within <code>obj</code>. * @param value the new value of the field. */ public native void putIntVolatile(Object obj, long offset, int value);
Volatile是会插入#storeload的内存屏障,效率略低内存
本篇为俺的课堂《Java基础:手写jdk》的前置知识,欢迎你们围观ci