package J20250730.demo09_synchronizedMethod;

/**
 * @author WanJl
 * @version 1.0
 * @title SellTicket
 * @description 同步方法
 * @create 2025/7/30
 */
public class SellTicket implements Runnable{
    //总共100张票
    private int tickets = 100;
    @Override
    public void run() {
        while (true){
            boolean b = synchronizedMethod();
            if (b) break;
        }
    }

    /**
     * 同步方法--售票
     * @return
     */
    public synchronized boolean synchronizedMethod(){
        if (tickets==0){
            return true;
        }else {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }//显示XX窗口成功售票
            System.out.println(Thread.currentThread().getName()+"售票1张，还剩"+ --tickets+"张票");
            return false;
        }
    }
}
