package J20250730.demo08_synchronized;

/**
 * @author WanJl
 * @version 1.0
 * @title SellTicket
 * @description
 * @create 2025/7/30
 */
public class SellTicket implements Runnable{
    //总共100张票
    private int tickets = 100;
    Object o=new Object();
    @Override
    public void run() {
        while (true){
            //新增一个同步代码块
            synchronized (o){
                if (tickets>0){
                    //出票过程睡10毫秒
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //显示XX窗口成功售票
                    System.out.println(Thread.currentThread().getName()+"售票1张，还剩"+ --tickets+"张票");
                }
            }
        }
    }
}
