四个窗口卖票

package com.heima.lei;

public class Test04 {

    /**四个窗口卖票 * @param args */
    public static void main(String[] args) {
        Thread t1 = new MyTicket();
        Thread t2 = new MyTicket();
        Thread t3 = new MyTicket();
        Thread t4 = new MyTicket();
        t1.setName("第一号窗口:");
        t2.setName("第二号窗口:");
        t3.setName("第三号窗口:");
        t4.setName("第四号窗口:");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

}
class MyTicket extends Thread{
    private static int ticket = 100;
    public void run(){
        while(true){
            synchronized (MyTicket.class) {
                if(ticket == 0){
                    break;
                }
                try {
                    sleep(10);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
                System.out.println(getName()+"卖出"+ticket--+"号票!");
            }
        }
    }
}

你可能感兴趣的:(四个窗口卖票)