在售票程序中,为什么将Ticket中的 num变量 用static修饰后就可以正常合理运行了
/*
* 需求 多个窗口同时出售车票
*
*/
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--);
}
}
}
}
public class ThreadDemo02
{
public static void main(String[] args)
{
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();
}
}
|
|