java - 做 "nothing"而 "condition"

标签 java while-loop do-while

在浏览 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/

相关文章:

java - 保留正则表达式解析的数据

VBA : Exit While in If

java - Java Applet 中的 Apache FOP - 未找到数据的 ImagePreloader

c++ - 打破 while 循环重复

while-loop - Kotlin - 将 while 循环转换为函数式样式

javascript - 在 do-while 循环中使用 continue

Java:奇怪的 OutOfBoundsException

java - 有没有办法限制 do-while 循环中的一行代码只完成一次?

java - fork/join,输入数组是否需要同步?

java - 在java中显示联系人列表程序中的联系人