Memory Synchronization in a Multi-Core System
Since both threads run on separate cores (most likely), objects to which the threads have access are cached by each core (in L1 cache) to improve performance.
Whenever the state of an object changes, all caches try to synchronize with the value in RAM, which is not guaranteed immediately.
Some threads can synchronize in front of other threads, even if they break the relationship going on before between them.
The reason for this is to implement the java memory model .
The same thing happens with the System.out object here.
You cannot guarantee the order in which data is transmitted and passed through it.
Therefore, this is unpredictable. Call
System.out.flush();
can improve the result, but it does not guarantee.
For more information, see Multiple Threads Using System.out.println in Java
Hope this helps.
Tanmay patil
source share