运行结果只有一个售票窗口出票,是怎么回事?class SaleTicket implements Runnable { 
    int tickets = 100;     public void run() { 
      while (tickets > 0) { 
          sale();       } 
    }     public synchronized void sale() { 
      if (tickets > 0) { 
          System.out.println(Thread.currentThread().getName() + "卖第" 
                  + (100 - tickets + 1) + "张票"); //打印第几个线程正在执行
          tickets--; 
      } 
    } } public class TestSaleTicket {     public static void main(String[] args) { 
      SaleTicket st = new SaleTicket(); 
      Thread t1=new Thread(st, "一号窗口"); 
      Thread t2=new Thread(st, "二号窗口"); 
      Thread t3=new Thread(st, "三号窗口"); 
      Thread t4=new Thread(st, "四号窗口"); 
      Thread t5=new Thread(st, "五号窗口");
      t1.start();
      t2.start();
      t3.start();
      t4.start();
      t5.start();
    } 
}

解决方案 »

  1.   

    因为你的CPU跑得太快了!if (tickets > 0) {  
            System.out.println(Thread.currentThread().getName() + "卖第" + (100 - tickets + 1) + "张票"); //打印第几个线程正在执行
            tickets--; 
            try{
                Thread.sleep(1);//稍微停一会
            } catch(Exception e){}
         }  
      

  2.   

     SaleTicket st = new SaleTicket();  
    多个这个类的实例化吧应该是
      

  3.   

    try{
                Thread.sleep(1);//稍微停一会
            } catch(Exception e){}
         }  
    这个是停顿的命令是吧,受用了
      

  4.   

          try{
                Thread.sleep(1);//稍微停一会
            } catch(Exception e){}多停一会吧,sleep(10000)
      

  5.   

      public synchronized void sale() { 
    //加上延时
    Thread.sleep(500);
      

  6.   


    谢谢了先加入sleep后 可以多窗口了,只不过很多还是连续的窗口,就像一人买多张票!