生产者与消费者问题,wait与notify多线程协作出问题,请大家帮我纠正下。

sucbert 2009-08-01 08:37:28
生产者与消费者问题,wait与notify多线程协作出问题,请大家帮我纠正下。
产品:库存数量,库存最大值,等待标识。
生产者:数量+1,达到最大值时,等待,由消费者唤醒。
消费者:数量-1,达到0时,等待,由生产者唤醒。

目前问题:唤醒有问题,不能持续生产,消费活动。

贴上代码:

package PExClass;

import java.lang.Thread;

public class CPar {
public static void main(String[] args) {
Producer producer = new Producer();
producer.start();

Concumer concumer = new Concumer();
concumer.start();

}
}

class Product {
static int num=0;
static boolean bWait=false;
final static int MAX = 10;
}

class Producer extends Thread {
public void run() {
while (true) {
produce();
}
}

public synchronized void produce() {
try {
if (Product.num == Product.MAX) {
Product.bWait=true;
wait();
} else {
int num=++Product.num;
System.out.println(Thread.currentThread().getName()
+ ":produce:" + num);
if(Product.bWait){
Product.bWait=false;
notify();
}
}
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}

class Concumer extends Thread {
public void run() {
while (true) {
consume();
}
}

public synchronized void consume() {
try {
if (Product.num == 0) {
Product.bWait=true;
wait();
} else {
int num=--Product.num;
System.out.println(Thread.currentThread().getName()
+ ":consume:" + num);
if(Product.bWait){
Product.bWait=false;
notify();
}
}
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}


由于我目前分数只有这点。请大家见谅。
...全文
105 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sucbert 2009-08-01
  • 打赏
  • 举报
回复
谢谢.
xiaozejun 2009-08-01
  • 打赏
  • 举报
回复
我看别人都是嫌分数太少 没有人来 我给你一个例子 你自己参考一下吧
Product类
package cn.xiaozejun.xiancheng.ch005;

public class Product {

private int num = 0;

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

//这个方法用来生产产品
public synchronized void produce()
{
num++;
System.out.println("生产了一个产品...");
this.notifyAll();
}
//这个方法用来销售产品
public synchronized void sale()
{
num--;
System.out.println("销售了一个产品...");
this.notifyAll();
}

//这个方法用来根据当前的产品的数量判断是否应该生产产品
public synchronized void waitForProduce() throws Exception
{
while(num<1)
{
System.out.println("等待生产...");
this.wait();
}
}
//这个方法用来根据当前的产品的数量判断是否应该销售产品
public synchronized void waitForSale()throws Exception
{
while(num>10)
{
System.out.println("等待销售...");
this.wait();
}
}
}


Produce类

package cn.xiaozejun.xiancheng.ch005;

public class Procduce implements Runnable{

Product product = null;

public Procduce(Product product)
{
this.product = product;
}

public void run(){
try {
while(true)
{
Thread.sleep(100);
product.waitForSale();
System.out.println("正在准备生产...");
product.produce();
System.out.println("现在商品的数量:"+product.getNum());
}
} catch (Exception e) {
e.printStackTrace();
}


}

}


Sale类

package cn.xiaozejun.xiancheng.ch005;

public class Sale implements Runnable{

Product product = null;

public Sale(Product product)
{
this.product = product;
}

public void run() {
try {
while(true)
{
Thread.sleep(500);
product.waitForProduce();
System.out.println("正在准备销售...");
product.sale();
System.out.println("现在产品的数量:"+product.getNum());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


测试类

package cn.xiaozejun.xiancheng.ch005;

public class Test {

/**
* @param args
*/
public static void main(String[] args) throws Exception{
Product product = new Product();
Procduce produce = new Procduce(product);
Sale sale = new Sale(product);



new Thread(produce).start();
new Thread(sale).start();

Thread.sleep(5000);
System.exit(0);//安全退出
}

}


你自己参考一下 吧

62,620

社区成员

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

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