Thread written questions: I don't understand why pingpong is output instead of pongping

the following code outputs pingpong. I don"t quite understand why this System.out.print outputs
before t.start. My idea is that pongping and pingpong should be output randomly, not in order.

public class Test2 {

    public static void main(String args[]) {

        Thread t = new Thread() {

            public void run() {
                pong();
            }
        };

        t.start();
        System.out.print("ping");

    }

    static void pong() {

        System.out.print("pong");

    }
}
Jul.12,2022

Thread.start () simply sets the new thread to enter the "ready" state and waits for CPU scheduling before it executes.
and the next System.out.print () is almost (with a probability greater than 99%) considered immediate.
so, the end result is almost always pingpong.

the time it takes for CPU to execute an instruction is almost negligible, basically in microseconds.
the interval between scheduling and switching of CPU is basically above millisecond.
the ratio of these two times is related to the environment, but it is obviously quite large (it is impossible to spend too much time and waste resources on switching).

different JAVA versions, virtual machines, configurations, may not be effective (compilation optimization, instruction disorder, garbage collection, etc.).
however, the next instruction of the main thread is faster than the first instruction of the new thread and is basically certain.
there is no such thing as "random".


the two threads are not on the same starting line, and the main thread has already run to System.out.print ("ping"); this line, t thread has just started, t thread must go through some preparation. On multi-core CPU, the main thread will first get the System.out object, and then output ping first. We have to find a way to get the two threads to stand on the same starting line.

  

this should be the random output of pongping and pingpong.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bde26e-30732.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bde26e-30732.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?