gpt4 book ai didi

java - 做 "nothing"而 "condition"

转载 作者:IT老高 更新时间:2023-10-28 13:51:24 24 4
gpt4 key购买 nike

在浏览 Java 8 版本的 ForkJoinPool 的代码(与 Java 7 相比有一些有趣的变化)时,我遇到了这个结构(here):

do {} while (!blocker.isReleasable() &&
!blocker.block());

我很困惑为什么你会这样写而不是这样写

while (!blocker.isReleasable() &&
!blocker.block());

这只是一个语义/可读性的选择,因为您可以将第一个构造读取为 do "nothing"while "conditions"?还是我缺少一些额外的好处?

最佳答案

如果你阅读了文件顶部的注释,就在类声明的正下方,有一个部分解释了这个结构的使用:

Style notes

===========

[...]

There are several occurrences of the unusual "do {} while
(!cas...)" which is the simplest way to force an update of a
CAS'ed variable. There are also other coding oddities (including
several unnecessary-looking hoisted null checks) that help
some methods perform reasonably even when interpreted (not
compiled).

关于java - 做 "nothing"而 "condition",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24609564/

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