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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 乔九 中级黑马   /  2012-9-10 21:49  /  1662 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class ProducerConsumerDemo
{
    public static void mian(String[] args)
     {
         Resource r=new Resource();
         Producer pro=new Producer(r);
         Consumer con=new Consumer(r);
         Thread t1=new Thread(pro);
         Thread t2=new Thread(con);
         t1.start();
         t2.start();
     }
}

class Resource
{
  private String name;
  private int count=1;
  private boolean flag=false;
  public synchronized void set(String name)
   {
        if(flag)
          try{wait();}catch(Exception e){}
        this.name=name+"--"+count++;
        System.out.println(Thread.currentThread().getName()+" 生产

者"+this.name);
        flag=true;
        this.notify();        
   }
  public synchronized void out()
  {
      if(!flag)
        try{wait();}catch(Exception e){}
      System.out.println(Thread.currentThread().getName()+"消费

者"+this.name);
      flag=false;
      this.notify();
  }
class Producer implements Runnable
{
        private Resource res;
        Producer(Resource res)
        {
           this.res=res;
        }
        public void run()
        {
          while(true)
          {
                res.set("商品");
          }
        }
}
class Consumer implements Runnable
{
   private Resource res;
   Consumer (Resource res)
    {
       this.res=res;
     }
public void run()
{
     while(true)
      {
         res.out();
      }
}
}

}

3 个回复

倒序浏览
public class ProducerConsumerDemo {
        public static void mian(String[] args) {//这里写错了。main()否则编译器找不到入口
                Resource r = new Resource();
                Resource.Producer pro = new Resource().new Producer(r);//你的Producer类定义的是内部类所以要通过外部类调用
                Resource.Consumer con =  new Resource().new Consumer(r);//同理
                Thread t1 = new Thread(pro);
                Thread t2 = new Thread(con);
                t1.start();
                t2.start();
        }
}

class Resource {
        private String name;
        private int count = 1;
        private boolean flag = false;

        public synchronized void set(String name)
   {
        if(flag)
          try{wait();}catch(Exception e){}
        this.name=name+"--"+count++;
        System.out.println(Thread.currentThread().getName()+" 生产者"+this.name);
        flag=true;
        this.notify();        
   }

        public synchronized void out()
  {
      if(!flag)
        try{wait();}catch(Exception e){}
      System.out.println(Thread.currentThread().getName()+"消费者"+this.name);
      flag=false;
      this.notify();
  }

        class Producer implements Runnable {
                private Resource res;

                Producer(Resource res) {
                        this.res = res;
                }

                public void run() {
                        while (true) {
                                res.set("商品");
                        }
                }
        }

        class Consumer implements Runnable {
                private Resource res;

                Consumer(Resource res) {
                        this.res = res;
                }

                public void run() {
                        while (true) {
                                res.out();
                        }
                }
        }
}


在外部类的外部,要创建一个成员内部类对象,要首先建立一个外部类对象,然后再创建一个成员内部类对象。

                Outer out = new Outer();

                Outer.Inner in = out.new Inner();

            在本类内部生成内部类对象的方式:

                在静态方法中:Inner in = new Outer().new Inner();

                在非静态方法中:Inner in = this.new Inner();



回复 使用道具 举报
class ProducerConsumerDemo
{
    //首先,你的主函数方法的格式错了
public static void main(String[] args)
     {
         Resource r=new Resource();
   //你原来定义Producer和Consumer都放到了Resource类中,成了内部类。
   //将它们分开就好!
         Producer pro=new Producer(r);
         Consumer con=new Consumer(r);
         Thread t1=new Thread(pro);
         Thread t2=new Thread(con);
         t1.start();
         t2.start();
     }
}
class Resource
{
  private String name;
  private int count=1;
  private boolean flag=false;
  public synchronized void set(String name)
   {
        if(flag)
          try{wait();}catch(Exception e){}
        this.name=name+"--"+count++;
        System.out.println(Thread.currentThread().getName()+" 生产者"+this.name);
        flag=true;
        this.notify();        
   }
  public synchronized void out()
  {
      if(!flag)
        try{wait();}catch(Exception e){}
      System.out.println(Thread.currentThread().getName()+"消费者"+this.name);
      flag=false;
      this.notify();
  }
}
class Producer implements Runnable
{
        private Resource res;
        Producer(Resource res)
        {
           this.res=res;
        }
        public void run()
        {
            while(true)
          {
               res.set("商品");
          }
        }
}
class Consumer implements Runnable
{
   private Resource res;
   Consumer (Resource res)
     {
       this.res=res;
     }
public void run()
      {
          while(true)
          {
             res.out();
           }
       }
}
回复 使用道具 举报
class ProducerConsumerDemo
{
    public static void mian(String[] args)  打错了,是main,不是mian
     {
         Resource r=new Resource();
         Producer pro=new Producer(r);
         Consumer con=new Consumer(r);
         Thread t1=new Thread(pro);
         Thread t2=new Thread(con);
         t1.start();
         t2.start();
     }
}

class Resource
{
  private String name;
  private int count=1;
  private boolean flag=false;
  public synchronized void set(String name)
   {
        if(flag)  这应该是while循环,时每个醒来的线程重复判断标记
          try{wait();}catch(Exception e){}
        this.name=name+"--"+count++;
        System.out.println(Thread.currentThread().getName()+" 生产

者"+this.name);
        flag=true;
        this.notify(); 应该是notifyAll。
                            如果是notify只唤醒自己的线程,而我们需要唤醒对方的线程,
                            所以就用notifyAll唤醒所有线程
      
   }
  public synchronized void out()
  {
      if(!flag)  同上
        try{wait();}catch(Exception e){}
      System.out.println(Thread.currentThread().getName()+"消费

者"+this.name);
      flag=false;
      this.notify();同上
  }
class Producer implements Runnable
{
        private Resource res;
        Producer(Resource res)
        {
           this.res=res;
        }
        public void run()
        {
          while(true)
          {
                res.set("商品");
          }
        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马