//定义一个生产类
class Producer extends Thread
{  
     
    Producer()
    {
        setPriority(Thread.MAX_PRIORITY);
    }
     public void run()
     {
         Store store = new Store();
         for(int i=0;i<15;i++)
         {
            store.store_in();
         }
     }
}
//定义一个消费类
class Customer implements Runnable
{   
    long time = 100;
    Customer()
    {
    }
     public void run()
     {   
           try{
                Thread.sleep(time);
            }catch (InterruptedException e){throw new RuntimeException(e);} 
         Store store = new Store();
          for(int i=0;i<15;i++)
         {
            store.store_out();          
         }
     }
}public class Store
{
     private static int productors = 0;
     private final int MAX_STORE = 10;
     private final int MIN_STORE = 1;
//定义一个同步入货方法
     public synchronized void store_in()
        {  
            if (productors >= MAX_STORE)
             {
                 try {
       System.out.println("库存数量太多!!!!!");
       wait();
                } catch (Exception e){}        
             }
            else
                {
      productors ++;
                    System.out.println("执行入库,库存数量为"+ productors);
                    notifyAll();
                }
        } 
//定义一个同步取货方法
     public synchronized void store_out()
     {
           if (productors < MIN_STORE)
             { 
                 try {
                    System.out.println("库存数量不足!!!!!");
                     wait();
               }catch (Exception e){}   
             }
            else 
            {
                productors --;
                System.out.println("执行出库,库存数量为"+productors); 
                notifyAll(); 
             }
     }
//定义查库存方法
      public int getStore()
      {
          return productors;
      }
//主涵数,启动两个线程
     public static void main(String args [])
     {
         Producer p = new Producer();
         p.start();
         Store store = new Store();
         
        Thread c = new Thread(new Customer());
         c.start();
        System.out.println(store.getStore());
     } 
}请问Thread C 如何唤醒Producer P,尽量给出修改结果,join()方法起什么作用,如何调,十分感谢

解决方案 »

  1.   

    notify();
    notifyAll();public final void notifyAll()
    唤醒在此对象监视器上等待的所有线程。线程通过调用其中一个 wait 方法,在对象的监视器上等待。 
    直到当前线程放弃此对象上的锁定,才能继续执行被唤醒的线程。被唤醒的线程将以常规方式与在该对象上主动同步的其他所有线程进行竞争;例如,唤醒的线程在作为锁定此对象的下一个线程方面没有可靠的特权或劣势。 此方法只应由作为此对象监视器的所有者的线程来调用。有关线程能够成为监视器所有者的方法的描述,请参阅 notify 方法。
    抛出:
    IllegalMonitorStateException - 如果当前线程不是此对象监视器的所有者。
    另请参见:
    notify(), wait()
      

  2.   

    你想在哪里唤醒?就在哪里写上这个代码!Store store = new Store();
    notifyAll();// 这样就行
    for (int i = 0; i < 15; i++) {
        store.store_out();
    }
      

  3.   

    Store store = new Store();
    for (int i = 0; i < 15; i++) {
        store.store_out();
        notifyAll();// 这样也许更好
    }
      

  4.   

    join(): Waits for this thread to die.
    假如在b线程区块中调用a.join(),就会阻塞当前b线程直至a线程运行完毕public class Test {
        public static void main(String[] args) {
            bao b = new bao();        b.start();        try {
                Thread.sleep(5000);
                b.join(100);
            }
            catch (Exception e) {
            }        int i = 0;
            while (true) {
                System.out.println("mainThread: "
                        + Thread.currentThread().getName());
            }    }
    }class bao extends Thread {
        public void run() {
            while (true) {
                System.out.println("run()Thread: "
                        + Thread.currentThread().getName());
            }
        }
    } 以上例子副线程会一直运行直至5.1秒后停止阻塞 主副线程并行
      

  5.   

    危险...: // 定义一个同步入货方法
    public synchronized void store_in() {
    if (productors >= MAX_STORE) {
    try {
    System.out.println("库存数量太多!!!!!");
    wait();
    } catch (Exception e) {
    }
    } else {
    productors++;
    System.out.println("执行入库,库存数量为" + productors);
    notifyAll();
    }
    }
    // 定义一个同步取货方法
    public synchronized void store_out() {
    if (productors < MIN_STORE) {
    try {
    System.out.println("库存数量不足!!!!!");
    wait();
    } catch (Exception e) {
    }
    } else {
    productors--;
    System.out.println("执行出库,库存数量为" + productors);
    notifyAll();
    }
    }这段代码危险!  wait(); 方法最好放在一个条件判断循环(while)里面   要不然 在notifyAll唤醒两个以上线程 取 访问productors 的时候会出现你不希望的结果修改建议:// 定义一个同步入货方法
    public synchronized void store_in() {
    try {
    while (productors >= MAX_STORE) {
    System.out.println("库存数量太多!!!!!");
    wait();
    }
    productors++;
    System.out.println("执行入库,库存数量为" + productors);
    notifyAll();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } // 定义一个同步取货方法
    public synchronized void store_out() {
    try {
    while (productors < MIN_STORE) {
    System.out.println("库存数量不足!!!!!");
    wait();
    }
    productors--;
    System.out.println("执行出库,库存数量为" + productors);
    notifyAll();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }