class Ticked extends Thread
{
private static int tick = 100;
Object obj=new Object();
public void run()
{
while (true)
{
synchronized(obj)
{
if (tick>0)
{
try{Thread.sleep(10);}catch(Exception e) {}
System.out.println(this.getName()+"还剩"+tick--+"票");
;
}
}
}
}
}
class Tick
{
public static void main(String[] args)
{
Ticked t1 = new Ticked();
Ticked t2 = new Ticked();
t1.start();
t2.start();
}
}
为什么这样还能打出0号票来呢?求解!
|