Home > Back-end >  Have who can tell me why the debug code below with inconsistent results run?
Have who can tell me why the debug code below with inconsistent results run?

Time:01-22

Public class Main {
Public static void main (String [] args) throws InterruptedException {
The Test Test=new Test ();
Thread thread1=new Thread (() -> {
Try {
System. The out. Println (test. Change ());
{} catch InterruptedException (e)
e.printStackTrace();
}
});
Thread thread2=new Thread (() -> {
Try {
System. The out. Println (test. Change ());
{} catch InterruptedException (e)
e.printStackTrace();
}
});
Thread1. Start ();
TimeUnit. MILLISECONDS. Sleep (8);
Thread2. Start ();
}
}
Public class Test {
Private volatile int a=0;
Public int change () throws InterruptedException {
While (a==0) {
TimeUnit. MILLISECONDS. Sleep (10);
+;

}
Return a;
}

}
  • Related