gpt4 book ai didi

java - 公共(public)代码消除在进行 JIT 时会优化这种双重检查吗?

转载 作者:行者123 更新时间:2023-12-01 09:04:34 24 4
gpt4 key购买 nike

java.util.concurrent.AbstractQueuedSynchronzier中找到以下代码

if (((h = head) != null && (s = h.next) != null &&
s.prev == head && (st = s.thread) != null) ||
((h = head) != null && (s = h.next) != null &&
s.prev == head && (st = s.thread) != null ))
return st;

初衷似乎是检查两次以确保条件得到满足。

我的问题是:当这段代码命中 JIT 时,它是否会被优化掉,因为两次检查看起来完全相同?

最佳答案

My question is: when this piece of code hit JIT, will it be optimised out since the two check looks exactly the same ?

如果我们假设该长表达式中引用的字段不是 volatile ,那么,由于表达式中没有对字段进行赋值,因此 JIT 编译器可以假设没有其他值会改变他们。这意味着允许优化第二个内存读取。

JIT 编译器是否真的这样做可能是特定于平台的;即它取决于编译器版本等。

关于java - 公共(public)代码消除在进行 JIT 时会优化这种双重检查吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41391714/

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