Eri*_*aas 69 java while-loop do-while
在浏览JavaJ版本的ForkJoinPool的代码时(我从Java 7中进行了一些有趣的更改),我遇到了这个构造(这里):
do {} while (!blocker.isReleasable() &&
!blocker.block());
Run Code Online (Sandbox Code Playgroud)
我正在努力为什么你会这样写,而不是只是
while (!blocker.isReleasable() &&
!blocker.block());
Run Code Online (Sandbox Code Playgroud)
它只是一个语义/可读性选择,因为你可以读取第一个结构do "nothing" while "conditions"吗?或者我还缺少一些额外的好处?
Mic*_*Sim 54
如果你阅读文件顶部的注释,就在类声明的下面,有一节解释了这个结构的用法:
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).
Run Code Online (Sandbox Code Playgroud)
Eri*_*aas 45
ForkJoinPool大量使用compareAndSwap...从sun.misc.Unsafe大多数的出现do {} while (...)在ForkJoinPool可以-正如其他的答案中提到-是由根据标题样式笔记此评论解释说:
Run Code Online (Sandbox Code Playgroud)* There are several occurrences of the unusual "do {} while * (!cas...)" which is the simplest way to force an update of a * CAS'ed variable.
选择使用写一个while空体的a -loop do {} while (condition)似乎是一个主要的风格选择.这可能更清楚HashMap,恰好在Java 8中更新.
在Java 7中,HashMap您可以找到:
while (index < t.length && (next = t[index++]) == null)
;
Run Code Online (Sandbox Code Playgroud)
虽然围绕它的大部分代码也发生了变化,但很明显Java 8中的替换是这样的:
do {} while (index < t.length && (next = t[index++]) == null);
Run Code Online (Sandbox Code Playgroud)
第一个版本的缺点是,如果单独的分号被删除,它将根据以下行改变程序的含义.
如下所示,生成的字节码while (...) {}和do {} while (...);稍有不同,但不会以任何方式影响运行时的任何内容.
Java代码:
class WhileTest {
boolean condition;
void waitWhile() {
while(!condition);
}
void waitDoWhile() {
do {} while(!condition);
}
}
Run Code Online (Sandbox Code Playgroud)
生成的代码:
class WhileTest {
boolean condition;
WhileTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
void waitWhile();
Code:
0: aload_0
1: getfield #2 // Field condition:Z
4: ifne 10
7: goto 0
10: return
void waitDoWhile();
Code:
0: aload_0
1: getfield #2 // Field condition:Z
4: ifeq 0
7: return
}
Run Code Online (Sandbox Code Playgroud)
撇开任何潜在的性能优势,都有明显的可读性优势.
对于while (X) ;尾随分号乍一看并不总是显而易见的,您可能会对以下语句或语句在循环内部感到困惑.例如:
while (x==process(y));
if (z=x) {
// do stuff.
}
Run Code Online (Sandbox Code Playgroud)
很容易误解上面的内容,因为在循环中有if语句,即使你确实正确地读了它,也很容易认为这是编程错误而if应该在循环中.
随着do {} while(X);虽然它立即一目了然,没有身体的循环.