售票(多线程)

来源:互联网 发布:快速减肥 知乎 编辑:程序博客网 时间:2024/05/28 06:06
class Demo extends Thread{    private static int tickets = 20;    private String name;    Demo(String name)    {        super(name);    }    public void run()    {        /*        for(int x=20;x>0;x--)            System.out.println(Thread.currentThread().getName()+"tickets="+x);    //erro        */        while(true)        {            if(tickets>0)                System.out.println(Thread.currentThread().getName()+"卖出第"+tickets--+"张票");        }        /*while(tickets>0)        {                System.out.println(Thread.currentThread().getName()+"卖出第"+tickets--+"张票");        }*/    }}class ThreadTicketsDemo{    public static void main(String[] args)    {    Demo t1 = new Demo("1窗口");    //创建第一个线程    Demo t2 = new Demo("2窗口");    Demo t3 = new Demo("3窗口");    Demo t4 = new Demo("4窗口");    t1.start();    t2.start();    t3.start();    t4.start();    }}