Java基础 生产者和消费者问题

俺是小王子 2014-11-18 12:13:19
这是一道Java基础题,最近我也是夯实基础才偶然发现

源码来源 Java SE doc文档示例程序

我放到我笔记本上一运行发现死锁了。。。。Java并发一直是我头疼的问题,解释不清;大CSDN人才济济,大家能否帮小弟想出一种可能性,到底问题出在哪。。。。。

知识领域:Java并发编程

题目类型:低级生产者消费者模型

运行环境:Win8 + Eclipse + 四核CPU + 4G RAM (大家别笑,Linux环境还真没怎么用过。。。暂时还没那需求。。。)

代码:

Drop类,共享类 代表生产者和消费者共享的东西

/**
* Copyright (c) 2014 All rights reserved.
* You can use all code for free just for study.
* If you want to use this code for business. please contact me. Thank you.
* email:<a href="mailto:wangchengjack@163.com">wangchengjack@163.com</a>
*/
package com.test.study;

/**
* @author WangCheng
* 2014年11月17日 下午11:19:52
* TODO
*/
public class Drop {
// Message sent from producer
// to consumer.
private String message;
// True if consumer should wait
// for producer to send message,
// false if producer should wait for
// consumer to retrieve message.
private boolean empty = true;

public synchronized String take() {
// Wait until message is
// available.
while (empty) {
try {
wait();
} catch (InterruptedException e) {}
}
// Toggle status.
empty = true;
// Notify producer that
// status has changed.
notifyAll();
return message;
}

public synchronized void put(String message) {
// Wait until message has
// been retrieved.
while (!empty) {
try {
wait();
} catch (InterruptedException e) {}
}
// Toggle status.
empty = false;
// Store message.
this.message = message;
// Notify consumer that status
// has changed.
notifyAll();
}
}


Producter 类 生产者

/**
* Copyright (c) 2014 All rights reserved.
* You can use all code for free just for study.
* If you want to use this code for business. please contact me. Thank you.
* email:<a href="mailto:wangchengjack@163.com">wangchengjack@163.com</a>
*/
package com.test.study;

import java.util.Random;

/**
* @author WangCheng
* 2014年11月17日 下午11:28:36
* TODO
*/
public class Producter implements Runnable{
private Drop drop;

public Producter(Drop drop) {
this.drop = drop;
}

public void run() {
String importantInfo[] = {
"Mares eat oats",
"Does eat oats",
"Little lambs eat ivy",
"A kid will eat ivy too"
};
Random random = new Random();

for (int i = 0;
i < importantInfo.length;
i++) {
drop.put(importantInfo[i]);
try {
Thread.sleep(random.nextInt(5000));
} catch (InterruptedException e) {}
}
drop.put("DONE");
}
}


Consumer 类 消费者

/**
* Copyright (c) 2014 All rights reserved.
* You can use all code for free just for study.
* If you want to use this code for business. please contact me. Thank you.
* email:<a href="mailto:wangchengjack@163.com">wangchengjack@163.com</a>
*/
package com.test.study;

import java.util.Random;

/**
* @author WangCheng
* 2014年11月17日 下午11:35:07
* TODO
*/
public class Consumer implements Runnable{
private Drop drop;

public Consumer(Drop drop) {
this.drop = drop;
}

public void run() {
Random random = new Random();
for (String message = drop.take();
! message.equals("DONE");
message = drop.take()) {
System.out.format("MESSAGE RECEIVED: %s%n", message);
try {
Thread.sleep(random.nextInt(5000));
} catch (InterruptedException e) {}
}
}
}


Main函数

/**
* Copyright (c) 2014 All rights reserved.
* You can use all code for free just for study.
* If you want to use this code for business. please contact me. Thank you.
* email:<a href="mailto:wangchengjack@163.com">wangchengjack@163.com</a>
*/
package com.test.study;

/**
* @author WangCheng
* 2014年11月17日 下午11:39:33
* TODO
*/
public class ProductAndConsumerDemo {
public static void main(String[] args) {
Drop drop = new Drop();
(new Thread(new Producter(drop))).start();
(new Thread(new Producter(drop))).start();
}

}


一开始我是照葫芦画瓢,发现我的死锁了,索性copy它的代码,发现同样如此。。。。。

代码出处 https://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html
...全文
133 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
俺是小王子 2014-11-18
  • 打赏
  • 举报
回复
囧大了
ghx287524027 2014-11-18
  • 打赏
  • 举报
回复
楼主,你代码写错了吧,跟你链接中的代码不一样,比如main函数: public class ProducerConsumerExample { public static void main(String[] args) { Drop drop = new Drop(); (new Thread(new Producer(drop))).start(); (new Thread(new Consumer(drop))).start(); } }
yktd26 2014-11-18
  • 打赏
  • 举报
回复
这个你得有消费者去take来解锁啊 你main里面开两个生产者,生产一个就锁住了....

62,615

社区成员

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

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