多线程模拟售票系统2

package com.smbea.demo.thread;

public class SaleTicket3 {
    private static int count = 20;	// 总票数
    
    private static synchronized boolean sell(String node){
        if(0 < count){
            count--;
            System.out.println("第 " + node + " 窗口售出第 " + (20 - count) + " 张票,还剩 " + count + " 张票");
            
            return true;
        }
        
        return false;
    }
    
    static class Window extends Thread{	// 售票窗口
        String node;	// 售票窗口编号
        
        public Window(String 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) {
        new Window("A").start();
        new Window("B").start();
        new Window("C").start();
    }
}

  

原文地址:https://www.cnblogs.com/hapday/p/6263494.html