java: execution order messed up without multithreading involved

55 Views Asked by At
//Pseudocode

class A
{
//......

b.method1();
b.method1();
b.method1();

b.method2();
b.method2();
b.method2();

//......
}

class B{
//......
method1(){
    log("m1 called {} {}",this.hashCode(),currentThreadId)
}

method2(){
    log("m2 called {} {}",this.hashCode(),currentThreadId)
}
//......
}

expected: 3 lines of m1 log, followed by 3 m2 log

actual: sometimes m2 log gets printed in between m1 logs

this only happens on one of my colleague's machine, not anyone else's

and even on his machine, it only happens like 2 or 3 times out of 10 attempts

when the execution order gets messed up, we have looked at the log, the object of B hash code is the same and the thread id is the same.

so, it's inside one thread, on one object, the execution order is getting messed up.

I am only showing Pseudocode here, not the actual full code, because even with the real code, I can not get it to stably reproduce the problem on other machines.

is there any reason why this would be happening? is there any hidden jvm secret that could be causing this?

the jdk used on my colleague's machine is 1.8.0_131. it's the same on my machine, but mine can not reproduce the problem.

2020-09-21 update

added stack trace info into the log line in m2,

it shows that each time m2 is called, in the stack trace it's caller is the line that is calling m1 in ClassA

0

There are 0 best solutions below