java简单的卖票程序

来源:互联网 发布:我的凉山兄弟 知乎 编辑:程序博客网 时间:2024/05/16 09:58
/*简单的卖票程序多个窗口同时卖票*/class Ticket implements Runnable{private int tick = 100;Object obj = new Object();public void run(){while(true){synchronized(obj){if(tick>0){try{Thread.sleep(10);}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--);}}}}}public class TicketDemo{public static void main(String[] args){Ticket t = new Ticket();Thread t1 = new Thread(t);Thread t2 = new Thread(t);Thread t3 = new Thread(t);Thread t4 = new Thread(t);t1.start();t2.start();t3.start();t4.start();}}