黑马程序员技术交流社区

标题: 帮忙看看问题出在哪呀? [打印本页]

作者: panjone    时间: 2011-12-15 18:20
标题: 帮忙看看问题出在哪呀?
本帖最后由 panjone 于 2011-12-15 23:12 编辑

class Goods
{
    Goods()
        {
          
        }
       
        private String name;
        private static int no=1;
        public boolean flag=false;
        public String getName()
        {
          return name;
        }
        public void setName(String name)
        {
            this.name=name;
        }
        public void setGoods(String name)
        {
           this.name=name;
           no++;
        }
       
        public void outGoods()
        {
             System.out.println(no+"---------"+name+"------"+Thread.currentThread().getName());
        }
       
       
}

class Runsetgoods implements Runnable
{
      Runsetgoods(Goods g)
          {
             this.g=g;
          }
      private Goods g;

      public void run()
          {
                         if(g.flag==false)
                     {
                       while(true)
                      {
                         
                             while(g.getName()!=null)
                                    try{
                                        this.wait();
                                        }catch(Exception e)
                                        {
                                         
                                        }
                                 synchronized(g)
                                 {
                                if(g.getName()==null)
                      g.setGoods("香蕉");
                                  g.flag=true;
                                 notifyAll();
                             }
                       }
                    }
                  if(g.flag=true)
                  {
                     while(true)
                         {
                                while(g.getName()!=null)
                                    try{
                                        this.wait();
                                        }catch(Exception e)
                                        {
                                         
                                        }
                                        synchronized(g)
                                        {
                                        if(g.getName()==null)
                            g.setGoods("苹果");
                                        g.flag=false;
                                        notifyAll();
                                        }
                     }
                  }
               
          }
}

class Runoutgoods implements Runnable
{

   private Goods g;
   Runoutgoods(Goods g)
   {
      this.g=g;
   }
   
   
   public void run()
   {
     while(true)
         {
              if(g.getName()==null)
                      try{
                               this.wait();
                                  }catch(Exception e){}
                    synchronized(g)
                        {
          if(g.getName()!=null)
                  g.outGoods();
                  g.setName(null);
                  notifyAll();
                  }
         }
   }
   
  }
  
  
  class Product
  {
       public static void main(String[] args)
           {
                Goods g=new Goods();
                    Runsetgoods rs=new Runsetgoods(g);
                        Runoutgoods ro=new Runoutgoods(g);
                       
                        Thread t1=new Thread(rs);
                        Thread t2=new Thread(rs);
                        Thread t3=new Thread(ro);
                       
                        t1.start();
                        t2.start();
                        t3.start();
                  
           }
  }

作者: 小春同学    时间: 2011-12-15 19:02
本帖最后由 熊明春 于 2011-12-15 19:02 编辑

需求是怎样啊,问题又是怎样,说清楚啊,这样直接粘代码不好·····恕在下直言,勿怪{:soso_e183:}
作者: 闫江龙    时间: 2011-12-15 19:59
我真的无语了 错的真的很多………… 你自己读一遍吧 意思都不通啊
作者: songxingchao    时间: 2011-12-15 20:13
notifyAll() 都要改成 g. notifyAll() 。 因为这个函数是唤醒等待这个对象的线程 。
作者: 王柠    时间: 2011-12-15 20:30
  你的代码中其中有一个  if(g.flag=true)判断语句,  应该写成  if(g.flag==true),你用“=”成赋值的意思啦
作者: 吴上储    时间: 2011-12-15 20:50
本帖最后由 吴上储 于 2011-12-15 20:51 编辑

class Goods
{
    Goods()
        {
         
        }
        
        private String name;
        private static int no=0;
        public boolean flag=false;
        public String getName()
        {
          return name;
        }
        public void setName(String name)
        {
            this.name=name;
        }
        public void setGoods(String name)
        {
           this.name=name;
           no++;
        }
        
        public void outGoods()
        {
             System.out.println(no+"---------"+name+"------"+Thread.currentThread().getName());
        }
        
        
}
class Runsetgoods implements Runnable
{
      Runsetgoods(Goods g)
          {
             this.g=g;
          }
      private Goods g;
      public void run()
          {
                         if(g.flag==false)
                     {
                       while(true)
                      {
                        
                             while(g.getName()!=null)
                              synchronized(g)
                              {
                                    try{
                                     Thread.sleep(200);
                                        g.wait();
                                        }catch(Exception e)
                                        {
                                         e.printStackTrace();
                                        }
                              }
                                if(g.getName()==null)
                                 g.setGoods("香蕉");
                                  g.flag=true;
                                  synchronized(g)
                               {
                                   g.notifyAll();
                               }
                       }
                    }
                  if(g.flag=true)
                  {
                     while(true)
                         {
                                while(g.getName()!=null)
                                 synchronized(g)
                                 {
                                    try{
                                        g.wait();
                                        }catch(Exception e)
                                        {
                                         e.printStackTrace();
                                        }
                                 }
                                        if(g.getName()==null)
                            g.setGoods("苹果");
                                        g.flag=false;
                                        synchronized(g)
                                     {
                                         g.notifyAll();
                                     }
                     }
                  }
               
          }
}
class Runoutgoods implements Runnable
{
   private Goods g;
   Runoutgoods(Goods g)
   {
      this.g=g;
   }
   
   
   public void run()
   {
     while(true)
         {
              if(g.getName()==null)
               synchronized(g)
               {
                      try{
                               g.wait();
                                  }catch(Exception e){e.printStackTrace();}
               }
          if(g.getName()!=null)
                  g.outGoods();
                  g.setName(null);
                  synchronized(g)
               {
                   g.notifyAll();
               }
                  }
   }
   
  }
  
  
public class Product
  {
       public static void main(String[] args)
           {
                Goods g=new Goods();
                    Runsetgoods rs=new Runsetgoods(g);
                        Runoutgoods ro=new Runoutgoods(g);
                        
                        Thread t1=new Thread(rs);
                        Thread t2=new Thread(rs);
                        Thread t3=new Thread(ro);
                        
                        t1.start();
                        t2.start();
                        t3.start();
                  
           }
}

改这个头痛

作者: panjone    时间: 2011-12-15 23:12
谢谢 大家!!!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2