onSpinWait​() method of Thread class - Java 9

前端 未结 4 573
傲寒
傲寒 2021-02-05 01:59

While learning Java 9 features I came across a new method of Thread class, called onSpinWait​. As per javadocs, this method is used for this:

4条回答
  •  温柔的废话
    2021-02-05 02:33

    I just want to add my 2 cents after reading the document and the source code for it. This method might trigger some optimizations and might not - so care must be taken - you can't really rely on it - since this a hint to the CPU more than demand, probably there's no initial relying whatsoever either way... Meaning that it's actual source code looks like this:

    @HotSpotIntrinsicCandidate
    public static void onSpinWait() {}
    

    What this actually means is that this method is basically a NO-OP until it hits the c2 compiler in the JIT, according to this

提交回复
热议问题