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.