在run中的while里我已经判断了如果i<=0就跳出循环了,为什么我运行后 输出到0后,过一会儿又会随机的输出,比如从50开始输出,50,49,48 ...这样,什么原因
public class TicketDemo {
public static void main(String[] args)
{
Ticket t=new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
Thread t3=new Thread(t);
Thread t4=new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket implements Runnable
{
private int i=100;
Object obj=new Object();
public void run()
{
while(true)
{
synchronized (obj)
{
if(i>0)
{
try{Thread.sleep(10);}
catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"sale........."+i--);
}
}
if(i<=0)
break;
}
}
}
|
|