本帖最后由 风乐 于 2013-5-8 13:38 编辑
class Ticket implements Runnable
{
private int num = 100;
boolean flag = true;
public void run()
{
if (flag)
{
while (num>0) //为什么还是会出现0票呢
{
synchronized (this)
{
System.out.println(Thread.currentThread().getName()+"..."+num--);
}
}
}
else
{
while(num>0)
twosale();
}
}
public synchronized void twosale()
{
System.out.println(Thread.currentThread().getName()+"........."+num--);
}
}
class error2
{
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();
}
}
为什么打印结果会出现0票呢,num--不是先用再减么,那while(num>0)里打印结果,不应该不会出现0的结果么
|
|