你应该在进行produce时将customer线程wait,不是随便找个东西wait,这样当produce动作完毕后才唤醒那个消费线程!

解决方案 »

  1.   

    c.wait();不应该是将c wait,而应该将以cc为引数的两个线程实例wait!
      

  2.   

    是不是。不会产生‘宝马’挖?把生产者里的
             try
    {
            Thread.sleep(1000);
         }
         catch(Exception e)
         {

         }提到if(i=0)外面
      

  3.   

    我是要先判断是否已经生产car,如果生产了就wait Producer线程,执行Customer线程
      

  4.   

    /*
     * 创建日期 2005-3-31
     *
     * TODO 要更改此生成的文件的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    package test;/**
     * @author Administrator
     *
     * TODO 要更改此生成的类型注释的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    public class Test6
    {
    public static void main(String args[])
    {
    Car car=new Car();
    Producer pp=new Producer(car);
    new Thread(pp).start();
    new Thread(pp).start(); Customer cc=new Customer(car);
    new Thread(cc).start();

    new Thread(cc).start();
    }
    }
    class Producer implements Runnable
    {
    private Car c;
    private int i=0;
    public Producer(Car c)
    {
    this.c=c;
    }

    public void run()
    {
         System.out.println(Thread.currentThread().getName()+" "+"STEP1");  //
         while(true)
         {
    System.out.println(Thread.currentThread().getName()+" "+"STEP2");  //
    synchronized(c)
    {
      System.out.println(Thread.currentThread().getName()+" "+"STEP3");//
      if(c.getbProduce())
      {
    try
    {
        c.wait();
        if(i==0)
      {
        System.out.println(Thread.currentThread().getName()+" "+"STEP4");//
        c.setName("宝马");
         try
         {
            Thread.sleep(1000);
         }
         catch(Exception e)
         {

         }

         c.setMoney("10000");
         System.out.println(Thread.currentThread().getName()+" STEP5 ");//
                }
       if(i==1)
       {
          c.setName("红旗");
          c.setMoney("20000");
          System.out.println(Thread.currentThread().getName()+" STEP6 ");//
       }
    }
    catch(Exception e)
    {

    }
      }
      System.out.println(Thread.currentThread().getName()+" "+i); //

       i=(i+1)%2;
       System.out.println(Thread.currentThread().getName()+" "+i);//
       c.setbProduce(true);
       c.notifyAll();
       System.out.println(Thread.currentThread().getName()+ " STEP7 ");//

    }
    System.out.println(Thread.currentThread().getName()+" "+"OVER");//
      }
    }

    }
    class Customer implements Runnable
    {
    private Car c;
    public Customer(Car c)
    {
    this.c=c;
    }

    public void run()
    {
        System.out.println(Thread.currentThread().getName()+" "+"STEP1");//
        while(true)
        {
                     System.out.println(Thread.currentThread().getName()+" "+"STEP2");//
            synchronized(c)
            {
      System.out.println(Thread.currentThread().getName()+" "+"STEP3");//
      if(!c.getbProduce())
      {
    try
    {
       c.wait();
    }
    catch(Exception e)
    {

    }

      }
    System.out.println(Thread.currentThread().getName()+" "+c.getName()+" "+c.getMoney());
    c.setbProduce(false);
    c.notifyAll();
    System.out.println(Thread.currentThread().getName()+" "+"STEP4");//
             }
         }
    }
    }
    class Car
    {
    private String name;
    private String money;
    private boolean bProduce;
    public Car()
    {
    name="宝马";
    money="10000";
    bProduce=false;
    }

    public String getName()
    {
    return name;
    }

    public String getMoney()
    {
    return money;
    }
    public boolean getbProduce()
    {
    return bProduce;
    }

    public void setName(String name)
    {
    this.name=name;
    }

    public void setMoney(String money)
    {
    this.money=money;
    }
    public void setbProduce(boolean b)
    {
    bProduce=b;
    }
    }