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