package com;

/**
 * @author 冀帅
 * @date 2020/7/31-22:02
 *
 */
class windowTest22 extends Thread{
    private static int tickets = 100;
    private static Object  obj = new Object();
    @Override
    public void run() {
        while (true) {
           // synchronized (obj) {//此处不能用this,因为this代表了三个对象 ,所以锁不唯一 。静态obj可以用
            synchronized (WindowTest2.class){//WindowTest2类也是对象,所以可以用 Class clazz = Window2.class
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + "卖票:" + tickets);
                    tickets--;

                } else {
                    break;
                }
            }
        }

    }
}
public class WindowTest2 {
    public static void main(String[] args) {
        windowTest22 t1 = new windowTest22();
        windowTest22 t2 = new windowTest22();
        windowTest22 t3 = new windowTest22();
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();

    }
}