写的是生产和消费包子的问题,一个四个实体类:生产者,消费者,包子,篮子,篮子可以装6个包子,不过运行结果不是预想中的那样,会出现生产7个包子后开始消费的情况,还有第一次消费基本都会从第一个开始消费,可预想是像栈那样后进先出的,不知道哪里出问题了,请指教~~~
   还有,生产和消费两个类中添加延时,就是文中注释掉的部分,结果貌似会和预想的相近一些。
    练习写的代码,还没养成写注释的习惯,抱歉~~~~
public class Produce
{
public static void main(String[] args) 
{
Barsket b=new Barsket();
Pro p=new Pro(b);
Cus c=new Cus(b);
new Thread(p).start();
new Thread(c).start(); }
}
//生产者
class Pro implements Runnable
{
Barsket b;
Pro(Barsket b)
{
this.b=b;
}
public void run()
{
for (int i=1;i<=20 ; i++)
{
/*try
{
Thread.sleep((int)(Math.random()*200));
}
catch (InterruptedException e)
{
e.printStackTrace();
}*/ Bao bao=new Bao(i);
b.produce(bao);
System.out.println("生产了 "+bao.toString());
}
}
}
//消费者
class Cus implements Runnable
{
Barsket b;
Cus(Barsket b)
{
this.b=b;
}
public void run()
{
for (int i=1;i<=20 ; i++)
{
/*try
{
Thread.sleep((int)(Math.random()*200));
}
catch (InterruptedException e)
{
e.printStackTrace();
}*/ Bao bao=b.consume();
System.out.println("消费了 "+bao.toString());
}
}
}
//篮子,可以放6个包子
class Barsket 
{
int index =0;
Bao bao[]=new Bao[6];
static int num=0;
public synchronized void produce(Bao baozi)
{
while(index==6)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
this.notifyAll();
bao[index]=baozi;
index++;
//return bao[index];
}
public synchronized Bao consume()
{
while(index==0)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
this.notifyAll();
index--;
return bao[index];
}
}
//包子类
class Bao 
{
int i;
Bao(int i)
{
this.i=i;
}
public String toString()
{
return "包子:"+i;
}
}
运行结果:
例子中的结果是这样的(举例不一样,用的窝窝头,意思一样)多线程java

解决方案 »

  1.   

    为啥不把 public synchronized void produce(Bao baozi)这个方法里面的逻辑放在Pro类的run()方法里面 public synchronized Bao consume()放在Cus类里面??
      

  2.   

    我以前写的一个
    /**
     * 生产者-消费者问题
     * 
     * @author 
     * 
     */
    public class Test { public static void main(String[] args) { Stack s = new Stack();
    Producer p = new Producer(s);
    Consumer c = new Consumer(s);

    Thread t1 = new Thread(p);
    Thread t2 = new Thread(c);

    t1.start();
    t2.start();

    }}class Stack {
    int[] elements;
    int size; public Stack() {
    elements = new int[5];
    size = 0;
    } public synchronized void push(int data) {
    while (size < 0 || size >= elements.length) {
    try {
    System.out.println("producer wait...");
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    elements[size] = data;
    size++;
    System.out.println("push[" + (size - 1) + "]:" + data);
    this.notifyAll();
    } public synchronized int pop() {
    while (size <= 0) {//为什么用while,假如stack为空,有两个消费者线程同时等待在这里,然后生产一个东西,两个生产者会先后去取,数组越界
    try {
    System.out.println("consumer wait...");
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    size--;
    int value = elements[size];
    System.out.println("pop[" + size + "]:" + value);
    this.notifyAll();
    return value;
    }
    }class Consumer implements Runnable {
    Stack s; public Consumer(Stack s) {
    this.s = s;
    } @Override
    public void run() {
    for (int i = 0; i < 20; i++)
    s.pop();
    }
    }class Producer implements Runnable {
    Stack s; public Producer(Stack s) {
    this.s = s;
    } @Override
    public void run() {
    for (int i = 0; i < 20; i++)
    s.push(i);
    }
    }
      

  3.   

    好像是郝~~
    把输出放到BasketBall  produce()方法 和 Consumer()方法里面就达到预期了~~~