java9系列(七)Variable Handles

本文主要研究下JEP 193: Variable Handles

Variable Handles

Variable Handles的API主要是用来取代java.util.concurrent.atomic包以及sun.misc.Unsafe类的功能。一个variable handle是一个variable的类型引用,用来在一系列访问模式下来读写variable。支持的variable包括实例变量,静态成员,数据元素等。Variable Handles需要依赖jvm的增强及编译器的协助,即需要依赖java语言规范及jvm规范的升级。

实例

目标类

    public static class Demo {
        public int count = 1;
        protected long sum = 100;
        private String name = "init";
        public int[] arrayData = new int[]{3,5,7};

        @Override
        public String toString() {
            return "Demo{" +
                    "name='" + name + '\'' +
                    ", count=" + count +
                    ", sum=" + sum +
                    ", data=" + Arrays.toString(arrayData) +
                    '}';
        }
    }
复制代码

访问public成员

    @Test
    public void testSetPublicField() throws NoSuchFieldException, IllegalAccessException {
        Demo instance = new Demo();
        VarHandle countHandle = MethodHandles.lookup()
                .in(Demo.class)
                .findVarHandle(Demo.class, "count", int.class);
        countHandle.set(instance,99);
        System.out.println(instance.count);
    }
复制代码

输出

99
复制代码

访问proteced成员

    @Test
    public void testSetProtectedField() throws NoSuchFieldException, IllegalAccessException {
        Demo instance = new Demo();
        VarHandle countHandle = MethodHandles.lookup()
                .in(Demo.class)
                .findVarHandle(Demo.class, "sum", long.class);
        countHandle.set(instance,99999);
        System.out.println(instance);
    }
复制代码

输出

Demo{name='init', count=1, sum=99999, data=[3, 5, 7]}
复制代码

访问private成员

    @Test
    public void testSetPrivateField() throws NoSuchFieldException, IllegalAccessException {
        Demo instance = new Demo();
        VarHandle countHandle = MethodHandles.privateLookupIn(Demo.class,MethodHandles.lookup())
                .findVarHandle(Demo.class, "name", String.class);
        countHandle.set(instance,"hello world");
        System.out.println(instance);
    }
复制代码

输出

Demo{name='hello world', count=1, sum=100, data=[3, 5, 7]}
复制代码

访问数组类型

    @Test
    public void testSetArray(){
        Demo instance = new Demo();
        VarHandle arrayVarHandle = MethodHandles.arrayElementVarHandle(int[].class);
        arrayVarHandle.compareAndSet(instance.arrayData,0,3,100);
        arrayVarHandle.compareAndSet(instance.arrayData,1,5,300);
        System.out.println(instance);
    }
复制代码

输出

Demo{name='init', count=1, sum=100, data=[100, 300, 7]}
复制代码

access modes

主要的访问模式有如下几种:

read access modes

such as reading a variable with volatile memory ordering effects; 主要有如下几个方法:get, getVolatile, getAcquire, getOpaque.

  • get

with memory semantics of reading as if the variable was declared non-{@code volatile}. Commonly referred to as plain read access.

  • getVolatile

用于读取volatile修饰的变量

  • getAcquire

ensures that subsequent loads and stores are not reordered before this access.

  • getOpaque

accessed in program order, but with no assurance of memory ordering effects with respect to other threads.

write access modes

such as updating a variable with release memory ordering effects; 主要有如下几个方法:set, setVolatile, setRelease, setOpaque.

atomic update access modes

such as a compare-and-set on a variable with volatile memory order effects for both read and writing; 主要有如下几个方法:compareAndSet, weakCompareAndSetPlain, weakCompareAndSet, weakCompareAndSetAcquire, weakCompareAndSetRelease, compareAndExchangeAcquire, compareAndExchange, compareAndExchangeRelease, getAndSet, getAndSetAcquire, getAndSetRelease.

numeric atomic update access modes

such as get-and-add with plain memory order effects for writing and acquire memory order effects for reading. 主要有如下几个方法:getAndAdd, getAndAddAcquire, getAndAddRelease

bitwise atomic update access modes

such as get-and-bitwise-and with release memory order effects for writing and plain memory order effects for reading. 主要有如下几个方法:getAndBitwiseOr, getAndBitwiseOrAcquire, getAndBitwiseOrRelease, getAndBitwiseAnd, getAndBitwiseAndAcquire, getAndBitwiseAndRelease, getAndBitwiseXor, getAndBitwiseXorAcquire, getAndBitwiseXorRelease.

小结

java9废弃了sun.misc.Unsafe类,引入了VarHandle作为替代。关于access modes部分涉及了JVM的内存模型,需要了解内存可见性、指令重排序等,才能使用好相关api。

doc

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值