A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© panjone 黑马帝   /  2011-12-15 18:20  /  1530 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 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();
                  
           }
  }

评分

参与人数 1技术分 +1 收起 理由
admin + 1 尽量用文字说明下!

查看全部评分

6 个回复

倒序浏览
本帖最后由 熊明春 于 2011-12-15 19:02 编辑

需求是怎样啊,问题又是怎样,说清楚啊,这样直接粘代码不好·····恕在下直言,勿怪{:soso_e183:}

评分

参与人数 1技术分 +1 收起 理由
admin + 1 对!

查看全部评分

回复 使用道具 举报
我真的无语了 错的真的很多………… 你自己读一遍吧 意思都不通啊
回复 使用道具 举报
notifyAll() 都要改成 g. notifyAll() 。 因为这个函数是唤醒等待这个对象的线程 。
回复 使用道具 举报
王柠 黑马帝 2011-12-15 20:30:22
报纸
  你的代码中其中有一个  if(g.flag=true)判断语句,  应该写成  if(g.flag==true),你用“=”成赋值的意思啦
回复 使用道具 举报
本帖最后由 吴上储 于 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();
                  
           }
}

改这个头痛

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
panjone 黑马帝 2011-12-15 23:12:08
7#
谢谢 大家!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马