Unsafe -> VarHandle

今天看并發(fā)編程的藝術(shù)的時候,拿出jdk的源碼對照著看,
然后發(fā)現(xiàn)AbstractQueuedSynchronizer的類中,compareSetTail方法如下

/**
 * CASes tail field.
 */
private final boolean compareAndSetTail(Node expect, Node update) {
    return TAIL.compareAndSet(this, expect, update);
}

直接返回的是一個TAIL.compareAndSet,

其中TAIL為 private transient volatile Node tail;
VarHandle類中


public final native
@MethodHandle.PolymorphicSignature
@HotSpotIntrinsicCandidate
boolean compareAndSet(Object... args);

而書中對這一段的描述,則是


QQ截圖20180901113011.png

調(diào)用的是unsafe類里的方法


網(wǎng)上搜了一波,

However JEP 260 proposes to leave sun.misc.Unsafe accessible due to its critical functionality, and also to gradually provide alternative functionality. There is an awful lot to sun.misc.Unsafe. Some, but not all of the funtionality will be provided by VarHandles. ————在java 9 中,VarHandle在許多地方替代了Unsafe的使用,畢竟unsafe是'unsafe'的,但并不是所有unsafe的功能都在VarHandle中實(shí)現(xiàn)了

VarHandle的定位是:

1.this will allow you to use Java constructs to arrange atomic or ordered operations on fields of individual classes
2.VarHandle intends to abstract the safe access to a memory location. It has methods for atomic operations, and a method descriptor type for each operation
3.VarHandle API aims to be as least as usable as sun.misc.Unsafe with comparable performance results, and better performance results than Atomic classes.

其中,unsafe中的

public final void lazySet(V newValue) {
    unsafe.putOrderedObject(this, valueOffset, newValue);
}

public final boolean compareAndSet(V expect, V update) {
    return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
}

在java9 中變成了這個以下代碼;

public final void lazySet(V newValue) {
    VALUE.setRelease(this, newValue);
}

public final boolean compareAndSet(V expectedValue, V newValue) {
    return VALUE.compareAndSet(this, expectedValue, newValue);
}

其中,VALUE為

private static final VarHandle VALUE;
static {
    try {
        MethodHandles.Lookup l = MethodHandles.lookup();
        VALUE = l.findVarHandle(AtomicReference.class, "value", Object.class);
    } catch (ReflectiveOperationException e) {
        throw new Error(e);
    }
}

看到了就先記下來,以后有機(jī)會整理更詳細(xì)的區(qū)別

參考文獻(xiàn):

  1. Correct way to use VarHandle in Java 9?
  2. Using JDK 9 Memory Order Modes by Doug Lea.
  3. Java 9 series: Variable Handles

————————————————————————————————————————————
PS:

AQS中的addWaiter也發(fā)生了變化:

QQ圖片20180901151650.png
private Node addWaiter(Node mode) {
        Node node = new Node(mode);
        //自旋,將node節(jié)點(diǎn)加到等待隊列中
        for (;;) {
            Node oldTail = tail;
            if (oldTail != null) {
                node.setPrevRelaxed(oldTail);
                if (compareAndSetTail(oldTail, node)) {
                    oldTail.next = node;
                    return node;
                }
            } else {
                //初始化頭結(jié)點(diǎn)
                initializeSyncQueue();
            }
        }
    }


 /**
     * Initializes head and tail fields on first contention.
     */
    private final void initializeSyncQueue() {
        Node h;
        if (HEAD.compareAndSet(this, null, (h = new Node())))
            tail = h;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容