java多线程实例1-

多线程实现四个窗口卖100张票

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 TicketDemo
{
    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();
    }
    
}

标签: none