ADDITIONAL SYSTEM INFORMATION :
jdk version: jdk 1.8.231 and jdk 11.0.5 oracle official version.
os: mac and windows 10 x64.
A DESCRIPTION OF THE PROBLEM :
the int variable change in the while loop, then output the result is wrong.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the example code.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
always output "-1".
ACTUAL -
after a lot of loop, output the "/".
---------- BEGIN SOURCE ----------
public class WhileLoopTest {
public void test1() {
int i = 8;
while ((i -= 3) > 0) ;
System.out.println("i = " + i);
}
public static void main(String[] args) {
WhileLoopTest tst = new WhileLoopTest();
for (int i = 0; i < 50_000; i++) {
tst.test1();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
just disable jit or use the AtomInteger.
public void test2() {
AtomicInteger i = new AtomicInteger(8);
while ((i.addAndGet(-3)) > 0) ;
System.out.println("i = " + i.intValue());
}
FREQUENCY : always
jdk version: jdk 1.8.231 and jdk 11.0.5 oracle official version.
os: mac and windows 10 x64.
A DESCRIPTION OF THE PROBLEM :
the int variable change in the while loop, then output the result is wrong.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the example code.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
always output "-1".
ACTUAL -
after a lot of loop, output the "/".
---------- BEGIN SOURCE ----------
public class WhileLoopTest {
public void test1() {
int i = 8;
while ((i -= 3) > 0) ;
System.out.println("i = " + i);
}
public static void main(String[] args) {
WhileLoopTest tst = new WhileLoopTest();
for (int i = 0; i < 50_000; i++) {
tst.test1();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
just disable jit or use the AtomInteger.
public void test2() {
AtomicInteger i = new AtomicInteger(8);
while ((i.addAndGet(-3)) > 0) ;
System.out.println("i = " + i.intValue());
}
FREQUENCY : always
- duplicates
-
JDK-8231988 Unexpected test result caused by C2 IdealLoopTree::do_remove_empty_loop
- Closed