package com.smbea.demo.thread;

public class SaleTicket2 {
    private static int count = 20;//总票数
    
    private static synchronized boolean sell(int node){
        if(0 < count){
            count--;
            System.out.println("第 " + node + " 窗口售出第 " + (20 - count) + " 张票,还剩 " + count + " 张票");
            
            return true;
        }
        
        return false;
    }
    
    static class Window extends Thread{	// 售票窗口
        int node;	// 售票窗口编号
        
        public Window(int node){
            this.node = node;
        }
        
        public void run(){
            while(sell(node)){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void main(String[] args) {
        for(int index = 1; index <= 10; index++){
            new Window(index).start();
        }
    }
}

  

相关文章:

  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
  • 2022-03-01
  • 2021-12-18
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2022-01-06
  • 2021-05-01
  • 2022-02-15
  • 2021-06-17
相关资源
相似解决方案