class Ticket extends Thread
{
private static int num=100;
public void run()
{
while(true)
{
if(num>0)
{
System.out.println(Thread.currentThread().getName()+".....sale...."+num--);
}
}
}
}
class ThreadDemo11
{
public static void main(String[] args)
{
Ticket t1=new Ticket();
Ticket t2=new Ticket();
Ticket t3=new Ticket();
Ticket t4=new Ticket();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
运行结果:(部分)
Thread-0.....sale....9
Thread-0.....sale....8
Thread-0.....sale....7
Thread-0.....sale....6
Thread-0.....sale....5
Thread-0.....sale....4
Thread-0.....sale....3
Thread-0.....sale....2
Thread-0.....sale....1
Thread-3.....sale....12
Thread-1.....sale....13
Thread-2.....sale....10
为什么输出num时12会在1后面?
|