AtomicInteger implementation and code duplication

后端 未结 5 714
眼角桃花
眼角桃花 2021-02-12 17:00

Warning: the question is a little long, but the part below the separation line is for curiosity only.

Oracle\'s JDK 7 implementation of AtomicInteger includes t

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-12 17:12

    To expand on @AlexeiKaigorodov's answer, if this is real Java code, it would be faster because it would eliminate an extra frame on the call stack. This makes it run quicker (why not?) and may have implications that multiple concurrent calls to the loop are less likely to fail, causing the loop to run repeatedly. (Though, I can't come up with any such reasons off the top of my head.)

    Though, through your micro-benchmarks, it's possible that the code is not real and the incrementAndGet() method is implemented in native code in the way you specified, or that both are just intrinsic instructions (delegated to lock:xadd on x86 for example). However, it's generally rather hard to divine what the JVM is doing all the time, and there could be other things that are causing this.

提交回复
热议问题