浏览ForkJoinPool的Java 8版本(与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).