四窗口卖票 实现Runnable

package com.heima.lei;

public class Test7 {

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

}
class MyRunnable1 implements Runnable{
    private  int ticket = 100;
    @Override
    public void run() {
        while(true){
            synchronized (MyRunnable1.class) {
                if(ticket == 0){
                    break;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "这是第" + ticket-- +"号票!");
            }
        }


    }

}

你可能感兴趣的:(四窗口卖票 实现Runnable)