生产者与消费者问题,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();
}
}
}
由于我目前分数只有这点。请大家见谅。
产品:库存数量,库存最大值,等待标识。
生产者:数量+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();
}
}
}
由于我目前分数只有这点。请大家见谅。
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);//安全退出
}}
你自己参考一下 吧