美文网首页
Unsafe -> VarHandle

Unsafe -> VarHandle

作者: 云中人山 | 来源:发表于2018-09-01 11:48 被阅读180次

今天看并发编程的艺术的时候,拿出jdk的源码对照着看,
然后发现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

调用的是unsafe类里的方法


网上搜了一波,

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中实现了

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);
    }
}

看到了就先记下来,以后有机会整理更详细的区别

参考文献:

  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也发生了变化:

QQ图片20180901151650.png
private Node addWaiter(Node mode) {
        Node node = new Node(mode);
        //自旋,将node节点加到等待队列中
        for (;;) {
            Node oldTail = tail;
            if (oldTail != null) {
                node.setPrevRelaxed(oldTail);
                if (compareAndSetTail(oldTail, node)) {
                    oldTail.next = node;
                    return node;
                }
            } else {
                //初始化头结点
                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;
    }

相关文章

网友评论

      本文标题:Unsafe -> VarHandle

      本文链接:https://www.haomeiwen.com/subject/xxnywftx.html