package p2; public class TestThread { public static void main(String args[]) throws InterruptedException{ Thread1 t1 = new Thread1(); new Thread(t1).start(); new Thread(t1).start(); new Thread(t1).start(); new Thread(t1).start(); t1.stop(); } } class Thread1 extends Thread{ private int tickets = 20; public void run(){ while(true){ synchronized(this){ if(tickets>0){ try { Thread.sleep(10l); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"售票"+tickets--); } else{ break; } } } } }
模拟售票功能,总票数是20张,同时分四个窗口售票