无数遍地看了生产者消费者的代码,但自己编写总会有问题,大家看看我的半段代码如何思考下去??

孤尽JavaSea 2009-09-25 06:47:54
class Test{
public static void main(String[] args) {
WareHouse ware = new WareHouse();
Producer pro = new Producer(ware);
Consumer con = new Consumer(ware);
System.out.println("lds");
pro.start();
con.start();
}
}


class WareHouse{
int number;
public synchronized void put(){
try {
wait();
} catch (Exception e) {
}
}
public synchronized void get(){
try {
notify();
} catch (Exception e) {
}
}
}
class Producer extends Thread{
public Producer(WareHouse proHouse){
}
}
class Consumer extends Thread{
public Consumer(WareHouse conHouse){
}

}
...全文
162 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
孤尽JavaSea 2009-10-08
  • 打赏
  • 举报
回复
谢谢了,说来说去还是wait 和 notify()没有理解透。
孤尽JavaSea 2009-09-26
  • 打赏
  • 举报
回复
能在我的代码基础上增加吗??
loveofmylife 2009-09-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 java_gannbare 的回复:]
能在我的代码基础上增加吗??
[/Quote]
我就是在你的方法上改的啊
loveofmylife 2009-09-25
  • 打赏
  • 举报
回复
重新贴一下上面的一段代码
if(number==MAX_SIZE){
this.wait();//如果满了则等待消费者消费
}
number++//不满的情况下继续生产;
this.notifyAll();//如果消费这线程等待中,可以解除它的等待状态
loveofmylife 2009-09-25
  • 打赏
  • 举报
回复
WareHouse基本不对,先定义一个WareHouse得最大容量
private final int MAX_SIZE;

你应该首先去判断WareHouse是不是满了或者空了
比如在put方法里

if(number==MAX_SIZE){
this.wait();//如果满了则等待消费者消费}
number++//不满的情况下继续生产;
this.notifyAll();//如果消费这线程等待中,可以解除它的等待状态

get方法就是判断WareHouse是否为空,和Put类似
Producer 在构造的时候传递一个WareHouse (就像你的main方法里那样)类中的run方法中,使用一个while循环一直的生产,即调用WareHouse里的put方法

public void run(){
while(true){
WareHouse.put();
Thread.sleep(500);}}

Consumer 类似Producer
dajiadebeibei9 2009-09-25
  • 打赏
  • 举报
回复
可以在class WareHouse里添加一个固定容量的数组或者新建一个类 在class WareHouse中new成数组
在put()和get()中要判断栈是否满或者是否空
在Producer和 Consumer中覆盖run()方法 将WareHouse的一个对象传进来 用循环不断的进行入栈和出栈操作
借你参考一下我原来写的

public class TicketTest {

/**
* @param args
*/
public static void main(String[] args) {
TicketStack ts = new TicketStack();
SoldTicket st = new SoldTicket(ts);
BuyTicket bt = new BuyTicket(ts);
new Thread(st).start();
new Thread(bt).start();

}

}

class Ticket {
int num;
public Ticket(int num) {
this.num = num;
}
}

/*存储票的栈*/
class TicketStack {
Ticket[] arrTicket = new Ticket[5];
int index = 0;

public synchronized void push(Ticket ticket) {
while(index == arrTicket.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
arrTicket[index] = ticket;
index++;
}

public synchronized Ticket pop() {
while(index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
index--;
return arrTicket[index];
}
}

//出票的人
class SoldTicket implements Runnable{
TicketStack ts = null;
public SoldTicket(TicketStack ts) {
this.ts = ts;
}

public void run() {
for(int i = 0;i<10;i++) {
Ticket t = ts.pop();
System.out.println("出票"+t.num);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


//买票的人
class BuyTicket implements Runnable{
TicketStack ts = null;
public BuyTicket(TicketStack ts) {
this.ts = ts;
}

public void run() {
for(int i =0;i<10;i++) {
Ticket t = new Ticket(i);
ts.push(t);
System.out.println("买票"+t.num);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
lxxzhy 2009-09-25
  • 打赏
  • 举报
回复
你的run方法呢?

62,614

社区成员

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

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