You know that for (;;) vs. while (true) that faster?

Come, for (;;) vs. while (true) What is the difference? From java semantics, they are exactly the same. Why how to say?

We started to test for (;;)

package com.tony.test;
import org.junit.Test;
/**
 * 测试循环
 *
 * @author tony
 * @create 2019-12-26 10:43
 **/
public class LoopTest {
    @Test
    public void testFor() {
        for (; ; ) {
            System.out.println("Tony Teacher test for");
        }
    }
}

Byte code output below

// class version 51.0 (51)
// access flags 0x21
public class com/tony/test/LoopTest {
  // compiled from: LoopTest.java
  // access flags 0x1
  public <init>()V
   L0
    LINENUMBER 11 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
    RETURN
   L1
    LOCALVARIABLE this Lcom/tony/test/LoopTest; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x1
  public testFor()V
  @Lorg/junit/Test;()
   L0
    LINENUMBER 16 L0
   FRAME SAME
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    LDC "Tony Teacher test for"
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
    GOTO L0
   L1
    LOCALVARIABLE this Lcom/tony/test/LoopTest; L0 L1 0
    MAXSTACK = 2
    MAXLOCALS = 1
}

We then test while (true)

package com.tony.test;
import org.junit.Test;
/**
 * 测试循环
 *
 * @author tony
 * @create 2019-12-26 10:43
 **/
public class LoopTest {
    @Test
    public void testWhile() {
        while (true) {
            System.out.println("Tony Teacher test while");
        }
    }
}

Byte code output below

// class version 51.0 (51)
// access flags 0x21
public class com/tony/test/LoopTest {
  // compiled from: LoopTest.java
  // access flags 0x1
  public <init>()V
   L0
    LINENUMBER 11 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
    RETURN
   L1
    LOCALVARIABLE this Lcom/tony/test/LoopTest; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x1
  public testFor()V
  @Lorg/junit/Test;()
   L0
    LINENUMBER 16 L0
   FRAME SAME
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    LDC "Tony Teacher test while"
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
    GOTO L0
   L1
    LOCALVARIABLE this Lcom/tony/test/LoopTest; L0 L1 0
    MAXSTACK = 2
    MAXLOCALS = 1
}

Online quoted a passage

Semantically, they're completely equivalent. It's a matter of taste, but I think while(true) looks cleaner, and is easier to read and understand at first glance. In Java neither of them causes compiler warnings.
At the bytecode level, it might depend on the compiler and the level of optimizations, but in principle the code emitted should be the same.

The two way are possible, depending on the level of compiler and optimizer, the principle is exactly the same.

Next interviewer ask you this question, you can go back and hate. By ASM, see byteCode code. The two goods are exactly alike. For me, I like to use for(;;), feel for the situation which certainly will exit the loop,

However while(true), a quick look will feel always will cycle continues.
Just look at people like, you like to use that kind of writing it?

Then talk about while (true)

When you use an empty loop when you look at your CPU usage will soon reach 100%, how to keep the CPU usage to 100%? Now, if the interviewer asks you, how would you answer?
Thread.sleep, Sleep on the method. Because while will consume all computing resources possible. If you now generate a dump file, you will find jvm garbage collection time is very frequent.

while (true)
{
    try
    {
        Thread.sleep(500);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

500 ms ms cycle time, you can not overlook the 500 milliseconds. Do you know a millisecond cpu can do a lot of things. Thread waits for you at the moment, you can make cpu shop and go to other things.

You guessed the answer? In fact, these problems, we usually look at the source code have seen. But you do not have to sum up why do you write? More know these know why. IT This road is long, lonely and boring, need to stop pondering learning. Stick with it, you will enjoy happiness and wealth coding brings.

Sweep the attention to the next, waiting for you to share more.

"Tony teacher" engaged in Internet research and development 10+ years, experienced a baptism of large and small companies. Regularly share technical article, I hope you pay attention to my colleagues, together we explore the technology life!

Guess you like

Origin www.cnblogs.com/tonyY/p/12101120.html