程序如下:
public class test{
public static void main(String[] args){
Box b = new Box();
Producer p = new Producer(b);
Consumer c = new Consumer(b);
new Thread(p).start();
new Thread(c).start();
}
}

class SteamedBun{
int id;
SteamedBun(int id){
this.id = id;
}
public String toString(){
return "SteamBun:" + id;
}
}class Box{
int index = 0;
int count = 0;
SteamedBun[] arrsb = new SteamedBun[6];

public synchronized void pull(SteamedBun sb){
while(index == arrsb.length){
try{
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.notify();
arrsb[index] = sb;
System.out.println("生产了" + sb);
index ++;
}

public synchronized void pop(SteamedBun sb){
while(index == 0){
try{
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
} this.notify();
index --;
arrsb[index] = sb;
System.out.println("消费了" + sb);
}
}


class Producer implements Runnable{
Box b = null;
Producer(Box b){
this.b = b;
}

public void run(){
for(int i = 0;i < 10;i++){
SteamedBun sb = new SteamedBun(i);
b.pull(sb);
try {
Thread.sleep(1000);
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


class Consumer implements Runnable{
Box b = null;
  Consumer(Box b){
this.b = b;
}

public void run(){
for(int i = 0;i < 10;i++){
SteamedBun sb = new SteamedBun(i);
b.pop(sb);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}输出地结果比如是生产1,2,之后消费是1,2
但是我想要的是生产后的消费时栈一样的消费,后生产的先消费!
谢谢各位大大了 !

解决方案 »

  1.   

    但是我想要的是生产后的消费时栈一样的消费,后生产的先消费!
    我没有明白这句话是什么意思生产消费者  java是可以通过阻塞队列来做的,而且效果特别好.
      

  2.   

    用栈不就行了?package cn.com.season1;import java.util.Stack;public class test{
    public static void main(String[] args){
    Box b = new Box();
    Producer p = new Producer(b);
    Consumer c = new Consumer(b);
    new Thread(p).start();
    new Thread(c).start();
    }
    } @SuppressWarnings("rawtypes")
    class SteamedBun extends Stack{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    int id;

    SteamedBun(){

    }

    SteamedBun(int id){
    this.id = id;
    }

    public String toString(){
    return "SteamBun:" + id;
    }
    } class Box{
    int index = 0;
    int count = 0;
    SteamedBun arrsb = new SteamedBun();

    @SuppressWarnings("unchecked")
    public synchronized void pull(SteamedBun sb){
    while(index == 6){
    try{
    this.wait();
    }catch(InterruptedException e){
    e.printStackTrace();
    }
    }

    this.notify();
    arrsb.push(sb);
    System.out.println("生产了" + sb);
    index ++;


    public synchronized void pop(){
    while(index == 0){
    try{
    this.wait();
    }catch(InterruptedException e){
    e.printStackTrace();
    }
    }

    this.notify();
    index --;
    SteamedBun sb = (SteamedBun)arrsb.pop();
    System.out.println("消费了" + sb);
    }
    }
    class Producer implements Runnable{
    Box b = null;
    Producer(Box b){
    this.b = b;
    }

    public void run(){
    for(int i = 0;i < 10;i++){
    SteamedBun sb = new SteamedBun(i);
    b.pull(sb);
    try {
    Thread.sleep(1000);
    Thread.sleep((int)(Math.random() * 200));
    } catch (InterruptedException e) {
    e.printStackTrace();


    }
    }
    class Consumer implements Runnable{
    Box b = null;
    Consumer(Box b){
    this.b = b;
    } public void run(){
    for(int i = 0;i < 10;i++){
    b.pop();

    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();


    }
    }