“什么都不做"而“条件" [英] Do "nothing" while "condition"

查看:24
本文介绍了“什么都不做"而“条件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While browsing the code for the Java 8 version of ForkJoinPool(which has a few interesting changes from Java 7) I ran across this construct (here):

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

我在为你为什么要这样写而不只是这样写而苦恼

I'm struggling with why you would write it like this instead of just

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

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

Is it just a semantics/readability choice, since you could read the first construct as do "nothing" while "conditions"? Or is there some additional benefit I'm missing?

推荐答案

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

If you read the comments at top of the file, just below the class declaration, there is a section which explains the use of this construct:

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).

这篇关于“什么都不做"而“条件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆