java吧 关注:1,300,754贴子:12,844,907
  • 1回复贴,共1

急!!!!!一个JAVA线程的问题??在线等???

取消只看楼主收藏回复

public class MyThread extends Thread
{
         static int TOTAL=5;
         final static int PRICE=5;
         int number;
         int money;
         public MyThread(int number,int money)
         {
                this.number=number;
                this.money=money;
         }
         public static void main(String args[])
         {
                  new MyThread(2,20).start();
                  new MyThread(1,20).start();
                  new MyThread(2,10).start();
                  new MyThread(1,10).start();
                  new MyThread(1,5).start();
         }
         public synchronized void run()
         {
                  int back=money-PRICE*number;
                  if(back>TOTAL)
                     
                      try{
                               wait(); 
                         }
                      catch(InterruptedException e)
                         {
                                 throw new RuntimeException(" cannot wait()");
                         }
                  System.out.println("Hello! "+Thread.currentThread().getName()+", this is your "+number+" ticket!we give you back"+back+"yuan");
                  TOTAL=TOTAL+PRICE*number;
                  notifyAll();
         }
}
==========================
请问Thread0和Thread1为什么没有执行呢?也就是前面两个线程没反应,只执行了后面三个线程,而且程序进入了停滞不前的状态??
请问错误在哪里呢?????


1楼2008-05-22 22:08回复
    那后面不是有了一个notifyAll了吗?
    他怎么不会被唤醒呢??


    3楼2008-05-23 12:11
    回复