class Demo
{
public static void main(String[] args)
{
Ticket t=new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag=false;
t2.start();
}
}
class Ticket implements Runnable
{ Object o=new Object();
private int num=100;
boolean flag=true;
public synchronized void show(){if(num>0){
System.out.println("售票台"+Thread.currentThread().getName()+"...sale1..."+num--);
}
}
public void run(){
if(flag){
while(true){
synchronized(o){
if(num>0){
System.out.println("售票台"+Thread.currentThread().getName()+"...sale2..."+num--);
}
}
}
}else{ while(true){
this.show();
}
}
}
}
出现了这种情况

我知道是因为用的不是一个锁但是出现顺序错误的原理是啥??