1.  package com.hebust.java.third; 
  2.  
  3. import java.util.Random; 
  4.  
  5. public class SaleTicket implements Runnable { 
  6.  
  7.     public int total; 
  8.     public int count; 
  9.  
  10.     public SaleTicket() { 
  11.         total = 100
  12.         count = 0
  13.     } 
  14.  
  15.     public void run() { 
  16.         while (total > 0) { 
  17.             synchronized (this) { 
  18.                 if(total > 0) { 
  19.                     try { 
  20.                         //Thread.sleep(800); 
  21.                         Thread.sleep(new Random().nextInt(1000)); 
  22.                     } catch (InterruptedException e) { 
  23.                         e.printStackTrace(); 
  24.                     } 
  25.                         count++; 
  26.                         total--; 
  27.                      
  28.                     System.out.println(Thread.currentThread().getName() + "\t当前票号:" + count); 
  29.                 } 
  30.             } 
  31.         } 
  32.  
  33.     } 
  34.  
  35.     public static void main(String[] args) { 
  36.         SaleTicket st = new SaleTicket(); 
  37.         for(int i=1; i<=5; i++) { 
  38.             new Thread(st, "售票点" + i).start(); 
  39.         } 
  40.     }