消费者、生产者问题之问题

wanglixue 2008-04-24 03:43:52
public class Test{
public static void main(String args[]){
Queue q = new Queue();
Producer p =new Producer(q);
Consumer c = new Consumer(q);
p.start();
c.start();
}
}
//生产者线程
class Producer extends Thread{
Queue q;
public Producer(Queue q){
this.q = q;
}
public void run(){
for(int i=0;i<10;i++){
q.putValue(i);
System.out.println("Producer put:"+q.getValue());
}
}
}
//消费者线程
class Consumer extends Thread{
Queue q;
public Consumer(Queue q){
this.q = q;
}
public void run(){
while(true){
System.out.println("Consumer get:"+q.getValue());
}
}
}
//队列类
class Queue{
int value;
boolean bFull = false;
public synchronized void putValue(int i){

if(!bFull){//首先判断队列中又没有东西,没有则生产
this.value = i;
bFull = true;
}
notify();//提醒消费者去消费
try{
wait();//等待消费者消费完了之后通知它来生产
}
catch(Exception e){
e.printStackTrace();
}


}
public synchronized int getValue(){
if(!bFull){//先判断队列中是否有东西,没有则等待
try{
wait();
}
catch(Exception e){
e.printStackTrace();
}
}
bFull = false;
notify();//此处不能将生产者唤醒。。。。???????、、、、、、、
return value;

}
}

程序运行之后发现,消费者消费完第一个后,不能将生产者唤醒。处于死锁状态,不知道怎么解决
...全文
89 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qing14 2008-04-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jiazhengjing 的回复:]
public void run(){
for(int i=0;i <10;i++){
q.putValue(i);
System.out.println("Producer put:"+q.getValue());
}
}
这句错了。改成System.out.println("Producer put:"+i);
[/Quote]

5楼是正确的,q.putValue(i);后唤醒了Consumer,然后Producer的System.out.println("Producer put:"+q.getValue());无法被唤醒
刘彬彬 2008-04-25
  • 打赏
  • 举报
回复
简单点吧,我贴出我写的代码给你看看:
public class Productor_buy {
int size = 4;

int remain = size;

public static void main(String[] args) {
Productor_buy pb = new Productor_buy();
pb.start();

}

class Productor implements Runnable {

public void run() {
for (int i = 0; true; i++) {
product(i);
}
}

}

class buyer implements Runnable {
public void run() {
for (int i = 0; true; i++) {
buy(i);
}
}
}

public synchronized void product(int i) {
while (remain < 4) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
remain++;
System.out.println("被生产,库存为--->" + remain);
}
notify();
}

public synchronized void buy(int i) {
while (remain <= 4) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
remain--;
System.out.println("被消费,库存为--->" + remain);
}
notify();
}

public void start() {
Productor pt = new Productor();
buyer by = new buyer();
by.run();
pt.run();
}
}
我上课的时候写的,希望对您有帮助!
jiazhengjing 2008-04-24
  • 打赏
  • 举报
回复
说下我的理解过程:

当执行完生产者进程的q.putValue(i); 后就唤醒消费者进程,进入消费者的run()执行getvalue(),
打印出第一个产品就是0,线程完了就唤醒生产者线程,此时bFull为true了,就是空了。
生产者接下来还要执行下面的语句,含有getValue():
System.out.println("Producer put:"+q.getValue()); 因为是空,所以生产者线程也等待了
这样就生产者等待,消费者也等待了,死锁了。
jiazhengjing 2008-04-24
  • 打赏
  • 举报
回复
public void run(){
for(int i=0;i <10;i++){
q.putValue(i);
System.out.println("Producer put:"+q.getValue());
}
}
这句错了。改成System.out.println("Producer put:"+i);
chief_fu 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 henglin 的回复:]
引用楼主 wanglixue 的帖子:
bFull = false;
notify();//此处不能将生产者唤醒。。。。???????、、、、、、、
return value;

不在同步的方法里面????
[/Quote]

同意。
盒子danbo 2008-04-24
  • 打赏
  • 举报
回复
是不是没有指定唤醒的对象呀
盒子danbo 2008-04-24
  • 打赏
  • 举报
回复
哦 !!对不起,看错了````呵呵``




帮顶一下
盒子danbo 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用楼主 wanglixue 的帖子:]
bFull = false;
notify();//此处不能将生产者唤醒。。。。???????、、、、、、、
return value;
[/Quote]
不在同步的方法里面????

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧