考虑下一个创建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) { // ... } } }
但下一步呢?我的意思是,如何使用这个可变句柄?你能提供任何真实的例子吗?
解决方法
public final void lazySet(V newValue) { unsafe.putOrderedObject(this,valueOffset,newValue); } public final boolean compareAndSet(V expect,V update) { return unsafe.compareAndSwapObject(this,expect,update); }
在这里,该指针与字段偏移一起用于访问字段.但这是不安全的,因为这个字段的偏移量可能会很长,你可能实际上正在访问完全不同的内容.然而,以这种方式执行性能优势(它告诉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 @R_404_103@ 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); } }