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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 武维京 高级黑马   /  2014-6-2 10:23  /  1344 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package InputOputcom;

  2. import java.util.concurrent.locks.Condition;
  3. import java.util.concurrent.locks.Lock;
  4. import java.util.concurrent.locks.ReentrantLock;





  5. public class ProducerConsumerDemo1 {

  6.         public static void main(String[] args) {
  7.                 Resource1 r = new Resource1();
  8.                  Producer1 p =new  Producer1(r);
  9.                  Consumer1 c=new Consumer1(r);
  10.                  
  11.                  Thread t1 = new Thread(p);
  12.                  Thread t2 = new Thread(p);
  13.                  Thread t3 = new Thread(c);
  14.                  Thread t4 = new Thread(c);
  15.                  t1.start();
  16.                  t2.start();
  17.                  t3.start();
  18.                  t4.start();

  19.         }

  20. }


  21. class Resource1{
  22.         private String name;
  23.         private int count=1;
  24.         private boolean flag = false;
  25.         private Lock lock = new ReentrantLock();
  26.         private Condition con_pro = lock.newCondition();
  27.         private Condition con_con = lock.newCondition();
  28.         public  void set(String name)throws InterruptedException{
  29.                 lock.lock();
  30.                 try {
  31.                 while(flag) {
  32.                        
  33.                                 con_pro.await();
  34.                        
  35.                
  36.                 this.name=name+"--"+count++;
  37.                 System.out.println(Thread.currentThread().getName()+"生产者..."+this.name);
  38.                 flag=true;
  39.                 con_con.signal();
  40.                 }
  41.                 }
  42.                 finally{
  43.                         lock.unlock();
  44.                 }
  45.                
  46.         }
  47.         public  void out() throws InterruptedException{
  48.                 lock.lock();
  49.                 try{
  50.                 while(!flag) {
  51.                        
  52.                   con_con.await();
  53.                 System.out.println(Thread.currentThread().getName()+"消费者........."+this.name);
  54.                  flag = false;
  55.                 con_pro.signal();
  56.                 }
  57.                 }
  58.                 finally{
  59.                         lock.unlock();
  60.                 }
  61.         }
  62. }


  63. class Producer1 implements Runnable{
  64.          private Resource1 res;
  65.          Producer1(Resource1 res){
  66.                  this.res=res;
  67.                  
  68.          }
  69.          public void run(){
  70.                  while(true){
  71.                          try {
  72.                                 res.set("商品");
  73.                         } catch (InterruptedException e) {
  74.                                 // TODO Auto-generated catch block
  75.                                 e.printStackTrace();
  76.                         }
  77.                  }
  78.          }
  79. }


  80. class Consumer1 implements Runnable{
  81.         private  Resource1 res;
  82.         Consumer1(Resource1 res){
  83.                 this.res=res;
  84.         }
  85.         public void run(){
  86.                 while(true){
  87.                         try {
  88.                                 res.out();
  89.                         } catch (InterruptedException e) {
  90.                                 // TODO Auto-generated catch block
  91.                                 e.printStackTrace();
  92.                         }
  93.                 }
  94.         }
  95. }
复制代码

我在Eclipse中没有运行结果。
有没有大神告知下是什么问题?

评分

参与人数 1技术分 +1 收起 理由
轻语。 + 1

查看全部评分

10 个回复

倒序浏览
flag = false;试试把这句话改成 flag=true。还有把while(flag)里的await()和其他执行语句分开,还有变量名设合理一点,最好有需求分析和注解,看这代码看得蛋都碎了

评分

参与人数 1技术分 +1 收起 理由
轻语。 + 1 很给力!

查看全部评分

回复 使用道具 举报
最好养成边写代码边注释的过程,100行的代码能看过来,等以后工作或编写时上千上万行代码没代码自己都不知道自己写了什么
回复 使用道具 举报
我用ecilpse也不输出东西。语法没问题
你直接用cmd试试
回复 使用道具 举报
哎,为了分,给你回个详细的修改代码,就是按我上个回复改就行了
  1. import java.util.concurrent.locks.Condition;
  2. import java.util.concurrent.locks.Lock;
  3. import java.util.concurrent.locks.ReentrantLock;





  4. public class ProducerConsumerDemo1 {

  5.         public static void main(String[] args) {
  6.                 Resource1 r = new Resource1();
  7.                  Producer1 p =new  Producer1(r);
  8.                  Consumer1 c=new Consumer1(r);
  9.                  
  10.                  Thread t1 = new Thread(p);
  11.                  Thread t2 = new Thread(p);
  12.                  Thread t3 = new Thread(c);
  13.                  Thread t4 = new Thread(c);
  14.                  t1.start();
  15.                  t2.start();
  16.                  t3.start();
  17.                  t4.start();

  18.         }

  19. }


  20. class Resource1{
  21.         private String name;
  22.         private int count=1;
  23.         private boolean flag = false;
  24.         private Lock lock = new ReentrantLock();
  25.         private Condition con_pro = lock.newCondition();
  26.         private Condition con_con = lock.newCondition();
  27.         public  void set(String name)throws InterruptedException{
  28.                 lock.lock();
  29.                 try {
  30.                 while(flag) {
  31.                         
  32.                                 con_pro.await();
  33.                         
  34.                 }
  35.                 this.name=name+"--"+count++;
  36.                 System.out.println(Thread.currentThread().getName()+"生产者..."+this.name);
  37.                 flag=true;
  38.                 con_con.signal();
  39.                 }
  40.                
  41.                 finally{
  42.                         lock.unlock();
  43.                 }
  44.                
  45.         }
  46.         public  void out() throws InterruptedException{
  47.                 lock.lock();
  48.                 try{
  49.                 while(!flag) {
  50.                         
  51.                   con_con.await();
  52.                 }
  53.                 System.out.println(Thread.currentThread().getName()+"消费者........."+this.name);
  54.                  flag = true;
  55.                 con_pro.signal();
  56.                 }
  57.                
  58.                 finally{
  59.                         lock.unlock();
  60.                 }
  61.         }
  62. }


  63. class Producer1 implements Runnable{
  64.          private Resource1 res;
  65.          Producer1(Resource1 res){
  66.                  this.res=res;
  67.                  
  68.          }
  69.          public void run(){
  70.                  while(true){
  71.                          try {
  72.                                 res.set("商品");
  73.                         } catch (InterruptedException e) {
  74.                                 // TODO Auto-generated catch block
  75.                                 e.printStackTrace();
  76.                         }
  77.                  }
  78.          }
  79. }


  80. class Consumer1 implements Runnable{
  81.         private  Resource1 res;
  82.         Consumer1(Resource1 res){
  83.                 this.res=res;
  84.         }
  85.         public void run(){
  86.                 while(true){
  87.                         try {
  88.                                 res.out();
  89.                         } catch (InterruptedException e) {
  90.                                 // TODO Auto-generated catch block
  91.                                 e.printStackTrace();
  92.                         }
  93.                 }
  94.         }
  95. }
复制代码
回复 使用道具 举报
superob123 发表于 2014-6-2 12:23
哎,为了分,给你回个详细的修改代码,就是按我上个回复改就行了

晕上面的是没改成的,这才是改了的


  1. import java.util.concurrent.locks.Condition;
  2. import java.util.concurrent.locks.Lock;
  3. import java.util.concurrent.locks.ReentrantLock;





  4. public class ProducerConsumerDemo1 {

  5.         public static void main(String[] args) {
  6.                 Resource1 r = new Resource1();
  7.                  Producer1 p =new  Producer1(r);
  8.                  Consumer1 c=new Consumer1(r);
  9.                  
  10.                  Thread t1 = new Thread(p);
  11.                  Thread t2 = new Thread(p);
  12.                  Thread t3 = new Thread(c);
  13.                  Thread t4 = new Thread(c);
  14.                  t1.start();
  15.                  t2.start();
  16.                  t3.start();
  17.                  t4.start();

  18.         }

  19. }


  20. class Resource1{
  21.         private String name;
  22.         private int count=1;
  23.         private boolean flag = false;
  24.         private Lock lock = new ReentrantLock();
  25.         private Condition con_pro = lock.newCondition();
  26.         private Condition con_con = lock.newCondition();
  27.         public  void set(String name)throws InterruptedException{
  28.                 lock.lock();
  29.                 try {
  30.                 while(flag) {
  31.                         
  32.                                 con_pro.await();
  33.                         
  34.                 }
  35.                 this.name=name+"--"+count++;
  36.                 System.out.println(Thread.currentThread().getName()+"生产者..."+this.name);
  37.                 flag=true;
  38.                 con_con.signal();
  39.                 }
  40.                
  41.                 finally{
  42.                         lock.unlock();
  43.                 }
  44.                
  45.         }
  46.         public  void out() throws InterruptedException{
  47.                 lock.lock();
  48.                 try{
  49.                 while(!flag) {
  50.                         
  51.                   con_con.await();
  52.                 }
  53.                 System.out.println(Thread.currentThread().getName()+"消费者........."+this.name);
  54.                  flag =false;
  55.                 con_pro.signal();
  56.                 }
  57.                
  58.                 finally{
  59.                         lock.unlock();
  60.                 }
  61.         }
  62. }


  63. class Producer1 implements Runnable{
  64.          private Resource1 res;
  65.          Producer1(Resource1 res){
  66.                  this.res=res;
  67.                  
  68.          }
  69.          public void run(){
  70.                  while(true){
  71.                          try {
  72.                                 res.set("商品");
  73.                         } catch (InterruptedException e) {
  74.                                 // TODO Auto-generated catch block
  75.                                 e.printStackTrace();
  76.                         }
  77.                  }
  78.          }
  79. }


  80. class Consumer1 implements Runnable{
  81.         private  Resource1 res;
  82.         Consumer1(Resource1 res){
  83.                 this.res=res;
  84.         }
  85.         public void run(){
  86.                 while(true){
  87.                         try {
  88.                                 res.out();
  89.                         } catch (InterruptedException e) {
  90.                                 // TODO Auto-generated catch block
  91.                                 e.printStackTrace();
  92.                         }
  93.                 }
  94.         }
  95. }
复制代码
回复 使用道具 举报
Resource类中的while(flag)这一句出的问题,while只是控制循环标记的判断,但是你将生产不走也和休眠代码放在了一起,已启动可不得没用反应麽!
回复 使用道具 举报
学到了,学到了
回复 使用道具 举报
哥们 我只想说没有注解的代码 真心看不下去
回复 使用道具 举报
superob123 发表于 2014-6-2 11:21
flag = false;试试把这句话改成 flag=true。还有把while(flag)里的await()和其他执行语句分开,还有变量名 ...

呵呵   谢谢你改的代码 我知道哪错了!!!  当时着急问问题没想注释的问题!!!
下次提问  我会加注释的!!!谢谢指正!!!!
回复 使用道具 举报
这代码真是长啊。。。。。。。。。。。。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马