gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:19:18 26 4
gpt4 key购买 nike

我一直在研究 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 中使用它,以前在 Java 8 中使用 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);
}

其中 VALUEVarHandle 定义如下:

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

关于java - 在 Java 9 中使用 VarHandle 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43558270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com