Producer, consumer and Queue : Producer and consumer « Thread « Java Tutorial






import java.util.Vector;

class Producer extends Thread {
  Queue queue;
  Producer(Queue queue) {
    this.queue = queue;
  }
  public void run() {
    int i = 0;
    while(true) {
      queue.add(i++);
    }
  }
}
class Consumer extends Thread {
  String str;
  Queue queue;
  Consumer(String str, Queue queue) {
    this.str = str;
    this.queue = queue;
  }

  public void run() {
    while(true) {
      System.out.println(str + ": " + queue.remove());
    }
  }
}

class Queue {
  private final static int SIZE = 5;
  private Vector queue = new Vector();
  private int count = 0;
  
  synchronized void add(int i) {
    while(count == SIZE) {
      try {
        wait();
      }
      catch(InterruptedException ie) {
        ie.printStackTrace();
        System.exit(0);
      }
    }
    queue.addElement(new Integer(i));
    ++count;
    notifyAll();
  }
  synchronized int remove() {
    while(count == 0) {
      try {
        wait();
      }
      catch(InterruptedException ie) {
        ie.printStackTrace();
        System.exit(0);
      }
    }
    Integer iobj = (Integer)queue.firstElement();
    queue.removeElement(iobj);
    --count;
    notifyAll();
    return iobj.intValue();
  }
}

class ProducerConsumers {
  public static void main(String args[]) {
    Queue queue = new Queue();
    new Producer(queue).start();
    new Consumer("ConsumerA", queue).start();
    new Consumer("ConsumerB", queue).start();
    new Consumer("ConsumerC", queue).start();
  }
}








10.13.Producer and consumer
10.13.1.Producer and comsumer with DataInputStream and DataOutputStream
10.13.2.Producer and consumer based on ReadableByteChannel and WritableByteChannel
10.13.3.Producer, consumer and Queue
10.13.4.Synchronized Queue with Producer and Consumer
10.13.5.A queue(LinkedList) is used to coordinate work between a producer and a set of worker threads.