1.1.2 Java @ForceInline 注解用法

本文作为Disruptor专栏中第1.1.2内容

先来看看原文:

        如果需要HotSpot VM的方法或构造器不需要使用标准指令排列处理时,可以使用此注解让其强制排列。

        这个注解不能被滥用,它合理的用法是用来将其绑定到HotSpot VM的指定的方法或构造器中,用来提供一种排列策略。不能依赖该注解去避免VM的正常指令排列策略,在HotSpot VM中的少数情况下,它只能作为临时的应对方法,直到指令排列情况改善。

这个注解仅对通过boot loader加载的类中的方法和构造器有影响,其他的均会被忽略。

A method or constructor may be annotated as "force inline" if the standard inlining metrics are to be ignored when the HotSpot VM inlines the method or constructor.
This annotation must be used sparingly. It is useful when the only reasonable alternative is to bind the name of a specific method or constructor into the HotSpot VM for special handling by the inlining policy. This annotation must not be relied on as an alternative to avoid tuning the VM's inlining policy. In a few cases, it may act as a temporary workaround until the profiling and inlining performed by the HotSpot VM is sufficiently improved.
Implementation Note:
This annotation only takes effect for methods or constructors of classes loaded by the boot loader. Annotations on methods or constructors of classes loaded outside of the boot loader are ignored.

然后

这主要就说明了,这个注解是用来做自定义指令排列的,他也有自己的局限性,不能作为调整指令排列的核心,只能作为一种替代方案。

而地球人都知道,在JMM中,在重排有3种方式:

1:编译器重排

编译器重排就是在程序编译的过程中的单线程情况下,不影响结果,但对执行方式进行调整,优化程序步骤的方法。其中volatile关键字就是这个用法。

2:指令重排

这是在CPU使用了ILP并行技术将多条指令一起执行,在多条指令间没有依赖性的情况下,CPU可以根据情况不同顺序执行。

3:内存重排

由于在JMM概念中有个逻辑上的CPU本地内存的概念,所以会根据情况对实际指令进行排列,分开执行,而本地内存的情况就导致这种读取现象很难被找到规则。

而使用这个注解,在一定程度上能避免重排的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扶朕去网吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值