Search code examples
javajvmjitjvm-hotspotdead-code

Are empty loops with side effects in it's condition caught by dead code elimination?


Given the following code

var cachedInt = new ArrayBlockingQueue<Integer>(xxxxx);

while(true){
   while(cachedInt.offer(randomProvider.nextInt()));
   latch.await();
}

Will the jvm eventually eliminate the while loop because it has no body or does it recognize the side effect of the condition and keep the loop in place?


Solution

  • No the jvm will not "optimize" your method call away. Your condition will run repeatedly until it returns false and the side effects will happen as normal.