class TestThread{
public static void main(String[] args){
sell s=new sell();
new Thread(s).start();
new Thread(s).start();
new Thread(s).start();
new Thread(s).start();
}
}
class sell implements Runnable{
int num=100;
int i=1;
public void run(){
Thread.currentThread().setName("windows "+i);
i++;
Object o=new Object();
while(true){
synchronized(o){
if(num>0){
try{
Thread.sleep(1);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" selled the "+num+" tickets ");
num--;
}
else{
break;
}
}
}
}
}
为什么我的线程同步不了 结果还是会出现 0,-1,和-2