Producer and comsumer with DataInputStream and DataOutputStream : Producer and consumer « Thread « Java Tutorial






import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class MainClass {

  public static void main (String[] args) throws IOException {
    PipedOutputStream pout = new PipedOutputStream();
    PipedInputStream pin = new PipedInputStream(pout);

    NumberProducer fw = new NumberProducer(pout, 20);
    NumberConsumer fr = new NumberConsumer(pin);
    fw.start();
    fr.start();
  }
}

class NumberProducer extends Thread {
  private DataOutputStream theOutput;
  private int howMany;

  public NumberProducer(OutputStream out, int howMany) {
    theOutput = new DataOutputStream(out);
    this.howMany = howMany;
  }

  public void run() {
    try {
      for (int i = 0; i < howMany; i++) {
        theOutput.writeInt(i);
      }
    }
    catch (IOException ex) { System.err.println(ex); }
  }
}

 class NumberConsumer extends Thread {

  private DataInputStream theInput;

  public NumberConsumer(InputStream in) {
    theInput = new DataInputStream(in);
  }

  public void run() {

    try {
      while (true) {
        System.out.println(theInput.readInt());
      }
    }
    catch (IOException ex) {
      if (ex.getMessage().equals("Pipe broken")
        || ex.getMessage().equals("Write end dead")) {
        // normal termination
        return;
      }
      ex.printStackTrace();
    }
  }
}








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.