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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© せR3n、何必装纯 黑马帝   /  2011-11-14 22:27  /  1496 人查看  /  0 人回复  /   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]

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马