多线程模拟售票系统

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();
        }
    }
}

  

posted @ 2017-01-09 00:51  hapday  阅读(1025)  评论(0编辑  收藏  举报