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

© 坚强 黑马帝   /  2011-11-14 22:16  /  1892 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  我按照视频敲的代码,不知道哪里错了,只会运行3次就停了,求高手指点!
  1. class Resource
  2. {
  3.         private String name;
  4.         private int count=1;
  5.         private boolean flag =false;
  6.         public synchronized void set(String name)
  7.         {
  8.           if(flag)
  9.                   try{this.wait();}catch(Exception a){}
  10.           this.name=name+"--"+count++;
  11.          
  12.           System.out.println(Thread.currentThread().getName()+"...生产者...."+this.name);
  13.           flag=true;
  14.           this.notify();
  15.         }
  16.         public synchronized void out()
  17.         {
  18.           if(!flag)
  19.          try{this.wait();}catch(Exception a){}
  20.       System.out.println(Thread.currentThread().getName()+"...消费者...."+this.name);
  21.       flag=false;
  22.           this.notify();
  23.         }
  24. }
  25. //---------------------------------
  26. class Producer implements Runnable
  27. {
  28.         private Resource res;
  29.         Producer(Resource res)
  30.         {
  31.           this.res=res;
  32.         }
  33.         public void run()
  34.         {
  35.                 while(true)
  36.              {
  37.               res.set("商品");
  38.              }
  39.         }
  40. }
  41. //----------------------------------
  42. class Consumer implements Runnable
  43. {
  44.         private Resource res;
  45.         Consumer(Resource res)
  46.         {
  47.          this.res=res;
  48.         }
  49.         public void run()
  50.         {
  51.           res.out();
  52.         }
  53. }
  54. //------------------------------------
  55. class ProduceConsumerDemo
  56. {
  57.         public static void main(String[] args)
  58.         {
  59.                 Resource r =new Resource();
  60.                 Producer pro=new Producer(r);
  61.                 Consumer con=new Consumer(r);
  62.                  
  63.                 Thread t1=new Thread(pro);
  64.                 Thread t2=new Thread(con);
  65.                 t1.start();
  66.                 t2.start();
  67.         }
  68. }
复制代码

评分

参与人数 1技术分 +2 黑马币 +5 收起 理由
黑马谢承强 + 2 + 5

查看全部评分

3 个回复

倒序浏览
陈超 黑马帝 2011-11-14 22:52:00
沙发
本帖最后由 陈超 于 2011-11-14 22:54 编辑

Consumer类中的run()加入while循环就行了,42.class Consumer implements Runnable

43.{

44.        private Resource res;

45.        Consumer(Resource res)

46.        {

47.         this.res=res;

48.        }

49.        public void run()

50.        {

51.          res.out();//此处用while(true){res.out();}代替

52.        }

53.}

评分

参与人数 1技术分 +1 黑马币 +2 收起 理由
黑马谢承强 + 1 + 2

查看全部评分

回复 使用道具 举报
package poc;

public class Resource {
        private String name;
    private int count=1;
    private boolean flag =true;
    public synchronized void set(String name)
    {
            while(count<=100){
                if(!flag)
                        try{this.wait();}catch(Exception a){}
                this.name=name+"--"+count++;
     
                System.out.println(Thread.currentThread().getName()+"...生产者...."+this.name);
                flag=false;
                this.notify();
            }
    }
    public synchronized void out()
    {
            while(count<=100){
                if(flag)
                try{this.wait();}catch(Exception a){}
                System.out.println(Thread.currentThread().getName()+"...消费者...."+this.name);
                flag=true;
                this.notify();
            }
    }
}
//---------------------------------
package poc;

public class Producer implements Runnable {
         private Resource res;
     Producer(Resource res)
     {
       this.res=res;
     }
     public void run()
     {
             res.set("商品");
     }
}

//----------------------------------
package poc;

import org.omg.SendingContext.RunTime;

public class Consumer implements Runnable {
        private Resource res;
    Consumer(Resource res)
    {
     this.res=res;
    }
    public void run()
    {
      res.out();
    }
}
//------------------------------------
class ProduceConsumerDemo
{
        public static void main(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();
        }
}
给你正确的写法,你的while循环不对!
生产一个商品,消费一个商品,你只消费了一次,没有循环,当生产的线程进入wait()了需要消费来给其唤醒,因为没有循环了,怎么唤醒生产呢?!

评分

参与人数 1技术分 +1 黑马币 +1 收起 理由
黑马谢承强 + 1 + 1

查看全部评分

回复 使用道具 举报
坚强 黑马帝 2011-11-15 20:30:23
板凳
胡文杰 发表于 2011-11-15 00:20
package poc;

public class Resource {

恩  对  我照你的改好了  谢谢哦
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马