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());
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?
ForkJoinPool
makes extensive use ofcompareAndSwap...
fromsun.misc.Unsafe
and most of the occurrences ofdo {} while (...)
inForkJoinPool
can — as mentioned by other answers — be explained by this comment under the heading Style notes:The choice to use write a
while
-loop with an empty body asdo {} while (condition)
seems however to be a mostly stylistic choice. This is perhaps clearer inHashMap
, which happened to be updated in Java 8.In the Java 7
HashMap
you can find this:While much of the code around it has also changed, it is clear that the replacement in Java 8 is this:
The first version has the weakness that if the lone semicolon happened to be deleted it would change the meaning of the program depending on the following line.
As seen below, bytecode generated by
while (...) {}
anddo {} while (...);
is slightly different, but not in any way that should affect anything when run.Java code:
Generated code:
You can easily make something like this with:
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:
If you will read comment above the code, It is mentioned that...
If the caller is not a
ForkJoinTask
, this method is behaviorally equivalent toSo it is just another form to implement above code in else part...!!
In Style notes it is mentioned that,
And if you will see implementation of ManagedLocker#isReleasable, It is updating the lock and returns
true
if blocking is unnecessary.Interpretation :
Blank while loops are used to provide an interrupt until some condition reset to true/false.
Here,
do { } while(!...)
is a blocker/interrupt untilblocker.block()
will betrue
whenblocker.isReleasable()
isfalse
. Loop will continue execution whileblocker
is not releasable (!blocker.isReleasable()
) andblocker
is not blocked !! Execution will be out of loop as soon asblocker.block()
will set to true.Note that,
do{ } while(...)
does not update CAS variable, but it guarantee that program will wait until variable gets updated (force to wait until variable gets updated).Leaving aside any potential performance benefits, there is a clear readability benefit.
With
while (X) ;
the trailing semicolon is not always obvious at first glance, you may be confused into thinking that the following statement or statements are inside the loop. For example:It would be very easy to misread the above as having the if statement inside the loop, and even if you did read it correctly it would be easy to think that it was a programming mistake and the if should be inside the loop.
With
do {} while(X);
though it is immediately at a glance clear that there is no body to the loop.