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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄敏文 黑马帝   /  2011-8-24 17:02  /  2461 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

有个地方不解,请各位指点一下!!![code=java]
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerConsumerDemo2 {
public static void main(String[] args) {
  Resource2 r = new Resource2();
  
  Producer2 pro = new Producer2(r);
  Consumer2 con = new Consumer2(r);
  
  Thread t1 = new Thread(pro);
  Thread t2 = new Thread(pro);
  Thread t3 = new Thread(con);
  Thread t4 = new Thread(con);
  
  t1.start();
  t2.start();  
  t3.start();  
  t4.start();   
}
}
//资源
class Resource2 {
private String name;
private int count = 1;
private boolean flag = true;

private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();

public void set(String name) {
  lock.lock();
  /*============问题在这,请留意while语句的位置===========*/
  while (flag) {   
   try {     
    condition_pro.await();
    this.name = name + "......" + count++;
    System.out.println(Thread.currentThread().getName() + "....生产者...." + this.name);
   
    flag = true;
    condition_con.signal();
   } catch (InterruptedException e) {
    e.printStackTrace();
   } finally {
    lock.unlock();
   }
  }
}

public void out() {
  lock.lock();
  /*============问题在这,请留意while语句的位置===========*/
  while (!flag) {
   try {      
    condition_con.await();   
    System.out.println(Thread.currentThread().getName() + "....消费者........." + this.name);
   
    flag = false;
    condition_pro.signal();
   } catch (InterruptedException e) {
    e.printStackTrace();
   } finally {
    lock.unlock();
   }
  }
}
}
//生产者
class Producer2 implements Runnable {
private Resource2 res;

public Producer2(Resource2 res) {
  this.res = res;
}
@Override
public void run() {
  while (true) {
   res.set("++商品++");
  }
}

}
//消费者
class Consumer2 implements Runnable {
private Resource2 res;

public Consumer2(Resource2 res) {
  this.res = res;
}
@Override
public void run() {
  while (true) {
   res.out();
  }
}
}[/code]以上程序什么都没有输出,改为下面的代码后就有输出了,代码如下[code=java]
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerConsumerDemo2 {
public static void main(String[] args) {
  Resource2 r = new Resource2();
  
  Producer2 pro = new Producer2(r);
  Consumer2 con = new Consumer2(r);
  
  Thread t1 = new Thread(pro);
  Thread t2 = new Thread(pro);
  Thread t3 = new Thread(con);
  Thread t4 = new Thread(con);
  
  t1.start();
  t2.start();  
  t3.start();  
  t4.start();   
}
}
//资源
class Resource2 {
private String name;
private int count = 1;
private boolean flag = true;

private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();

public void set(String name) {
  lock.lock();
  /*============问题在这,请留意while语句的位置===========*/
   {   
   try {
    while (flag)
    condition_pro.await();
    this.name = name + "......" + count++;
    System.out.println(Thread.currentThread().getName() + "....生产者...." + this.name);
   
    flag = true;
    condition_con.signal();
   } catch (InterruptedException e) {
    e.printStackTrace();
   } finally {
    lock.unlock();
   }
  }
}

public void out() {
  lock.lock();
  /*============问题在这,请留意while语句的位置===========*/
   {
   try {
    while (!flag)
    condition_con.await();   
    System.out.println(Thread.currentThread().getName() + "....消费者........." + this.name);
   
    flag = false;
    condition_pro.signal();
   } catch (InterruptedException e) {
    e.printStackTrace();
   } finally {
    lock.unlock();
   }
  }
}
}
//生产者
class Producer2 implements Runnable {
private Resource2 res;

public Producer2(Resource2 res) {
  this.res = res;
}
@Override
public void run() {
  while (true) {
   res.set("++商品++");
  }
}

}
//消费者
class Consumer2 implements Runnable {
private Resource2 res;

public Consumer2(Resource2 res) {
  this.res = res;
}
@Override
public void run() {
  while (true) {
   res.out();
  }
}
}[/code]
[ 本帖最后由 黄敏文 于 2011-08-24  17:11 编辑 ]

评分

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

查看全部评分

1 个回复

倒序浏览
终于看出来了。。。
代码1中:  while 块包括整个try块。
         程序运行过程分析:flag 初始值为true,set()方法等待,即生产者线程等待,而在消费者线程中,只有一段while(!flag){....} 的代码,,flag为true,,当然执行不了,最终看起来就是生产者线程一直在等待。。

代码2中:    while 块包括一句线程等待语句。      
         程序运行过程分析: flag 初始值为true,set()线程等待。。消费者线程直接进行了消费out(), 消费后flag为false,导致消费者线程等待,执行生产者线程。。。。。。总的来说就是先消费后生产。。
      自己测试下,在Resource中的set()中  唤醒线程上加个条件就看到了先消费后生产的现象
                          if (count < 10) {       
                                condition_con.signal();
                        }
      要修正这个错误也很简单,,把flag的初始值 设为false即可
[ 本帖最后由 石宗银 于 2011-08-25  20:58 编辑 ]

评分

参与人数 1技术分 +2 收起 理由
wangfayin + 2 精神可嘉!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马