黑马程序员技术交流社区
标题:
生产者与消费者问题
[打印本页]
作者:
黄敏文
时间:
2011-8-24 17:02
标题:
生产者与消费者问题
有个地方不解,请各位指点一下!!![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 编辑
]
作者:
石宗银
时间:
2011-8-24 21:07
终于看出来了。。。
代码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 编辑
]
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2