在 Java 9 中使用 VarHandle 的正确方法?

Correct way to use VarHandle in Java 9?

我花了很多时间研究 Java 9 的 一些新功能,但我没有找到任何有用和实用的示例。

考虑下一个创建 VarHandle:

的代码片段
class Counter {
    int i;
}

class VarHandleInAction {
    static final VarHandle VH_COUNTER_FIELD_I;

    static {
        try {
            VH_COUNTER_FIELD_I = MethodHandles.lookup()
                .in(Counter.class)
                .findVarHandle(Counter.class, "i", int.class);
        } catch (Exception e) {
            // ...
        }
    }
}

但下一步是什么?我的意思是,如何使用这个 变量句柄 ?能举个真实的例子吗?

例如在AtomicReference中使用,之前在Java8中使用sun.misc.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);
}

这里使用this指针和字段偏移量来访问字段。但这是不安全的,因为这个字段偏移量可以是任何 long,并且您实际上可能正在访问完全不同的内容。然而,这样做有性能优势(例如,它告诉 VM 使用专门的 CPU 指令),因此其他人使用了 sun.misc.Unsafe,即使它是内部的,并且 不安全 API.

VarHandles 的部分目的是用安全的等价物替换 sun.misc.Unsafe 中的操作。 the JEP:

Define a standard means to invoke the equivalents of various java.util.concurrent.atomic and sun.misc.Unsafe operations...

Goals:

The following are required goals:

  • Safety. It must not be possible to place the Java Virtual Machine in a corrupt memory state. For example, a field of an object can only be updated with instances that are castable to the field type, or an array element can only be accessed within an array if the array index is within the array bounds.

  • Integrity. Access to a field of an object follows the same access rules as with getfield and putfield byte codes in addition to the constraint that a final field of an object cannot be updated. (Note: such safety and integrity rules also apply to MethodHandles giving read or write access to a field.)

  • Performance. The performance characteristics must be the same as or similar to equivalent sun.misc.Unsafe operations (specifically, generated assembler code should be almost identical modulo certain safety checks that cannot be folded away).

  • Usability. The API must be better than the sun.misc.Unsafe API.

所以在 Java 9 中,这些方法如下所示:

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 是一个 VarHandle 定义如下:

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