1 /* 2 * 售票系统 3 */ 4 package com.threadText; 5 public class Demo1 { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 //定义三个售票窗口 10 TicketWindow tw1 = new TicketWindow(); 11 12 Thread t1 = new Thread(tw1); 13 Thread t2 = new Thread(tw1); 14 Thread t3 = new Thread(tw1); 15 16 t1.start(); 17 t2.start(); 18 t3.start(); 19 } 20 21 } 22 23 class TicketWindow implements Runnable{ 24 private int nums = 2000; 25 26 @Override 27 public void run() { 28 // TODO Auto-generated method stub 29 while(true) { 30 synchronized (this) { 31 32 if(nums>0) { 33 System.out.println(Thread.currentThread().getName()+"在售出第"+nums+"张票"); 34 try { 35 Thread.sleep(1000); 36 } catch (Exception e) { 37 // TODO: handle exception 38 e.printStackTrace(); 39 } 40 nums--; 41 }else { 42 break; 43 } 44 } 45 } 46 47 } 48 49 }