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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 思维 高级黑马   /  2014-8-23 10:56  /  1080 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 思维 于 2014-8-25 13:41 编辑

今天写了一个生产者和消费者的代码,打印结果全部为null,谁能给分析下错误
  1. import java.util.concurrent.locks.*;
  2. class Demo{
  3.         public static void main(String[] args){
  4.                 Resource res = new Resource();
  5.                 Producer pro = new Producer(res);
  6.                 Consumer con = new Consumer(res);
  7.                 Thread t1 = new Thread(pro);
  8.                 Thread t2 = new Thread(pro);
  9.                 Thread t3 = new        Thread(con);
  10.                 Thread t4 = new Thread(con);
  11.                 t1.start();
  12.                 t2.start();
  13.                 t3.start();
  14.                 t4.start();
  15.         }
  16. }
  17. class Resource{
  18.         private String name;
  19.         private int count = 1;
  20.         private boolean flag = true;
  21.         private Lock lock = new ReentrantLock();
  22.         private Condition condition_pro = lock.newCondition();
  23.         private Condition condition_con = lock.newCondition();
  24.         public void set(String name)throws InterruptedException{
  25.                 lock.lock();
  26.                 try{
  27.                         while(flag)
  28.                                 condition_pro.await();
  29.                         this.name = name+"--"+count++;
  30.                         System.out.println(Thread.currentThread().getName()+"..生产者.."+this.name);
  31.                         flag = false;
  32.                         condition_con.signal();
  33.                 }finally{
  34.                         lock.unlock();
  35.                 }
  36.         }
  37.         public void out()throws InterruptedException{
  38.                 lock.lock();
  39.                 try{
  40.                         while(!flag)
  41.                                 condition_con.await();
  42.                         System.out.println(Thread.currentThread().getName()+"..消费者.."+this.name);
  43.                         flag = true;
  44.                         condition_pro.signal();
  45.                 }finally{
  46.                         lock.unlock();
  47.                 }
  48.         }
  49. }
  50. class Producer implements Runnable{
  51.         private Resource res;
  52.         Producer(Resource res){
  53.                 this.res = res;
  54.         }
  55.         public void run(){
  56.                 while(true){
  57.                         try{
  58.                                 Thread.sleep(1000);
  59.                                 res.set("+商品+");
  60.                         }catch(InterruptedException e){
  61.                                 e.printStackTrace();
  62.                         }
  63.                 }
  64.         }
  65. }
  66. class Consumer implements Runnable{
  67.         private Resource res;
  68.         Consumer(Resource res){
  69.                 this.res = res;
  70.         }
  71.         public void run(){
  72.                 while(true){
  73.                         try{
  74.                                 Thread.sleep(1000);
  75.                                 res.out();
  76.                         }catch(InterruptedException e){
  77.                                 e.printStackTrace();
  78.                         }
  79.                 }
  80.         }
  81. }
复制代码



捕获.JPG (75.45 KB, 下载次数: 14)

捕获.JPG

7 个回复

倒序浏览
你的flag那里有问题。
回复 使用道具 举报
何艳梅 发表于 2014-8-23 22:09
你的flag那里有问题。

我也知道那里有问题!主要是想不通为什么?
回复 使用道具 举报
0 0我也刚看到生产者和消费者= =我也去敲代码去了~~顺便研究下你的~
回复 使用道具 举报
private boolean flag = true;  改为false~~因为你下面如果运行的话直接就等待了~所以要么改FALSE要么改while里的条件
回复 使用道具 举报
丨懒蟲灬Nigh 发表于 2014-8-24 19:27
private boolean flag = true;  改为false~~因为你下面如果运行的话直接就等待了~所以要么改FALSE要么改whi ...

确实是这样啊!
回复 使用道具 举报
思维 发表于 2014-8-25 09:47
确实是这样啊!

那不就行了嘛?问题不是解决了吗?只是条件不小心写错了而已。
回复 使用道具 举报
想说细心太难啊。0-----0
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马