Why HotSpot will optimize the following using hoisting?

后端 未结 3 1720
闹比i
闹比i 2020-12-13 07:03

In the \"Effective Java\", the author mentioned that

while (!done) i++;

can be optimized by HotSpot into

if (!done) {
             


        
3条回答
  •  不思量自难忘°
    2020-12-13 07:49

    If you add System.out.println("i = " + i); in the while loop. The hoisting won't work, meaning the program stops as expected. The println method is thread safe so that the jvm can not optimize the code segment?

提交回复
热议问题