黑马程序员技术交流社区

标题: 多线程中static的应用 [打印本页]

作者: mirror    时间: 2014-7-10 13:01
标题: 多线程中static的应用
在售票程序中,为什么将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();
        }
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2