Filter Streams   «Prev  Next»

Lesson 9Streaming data between threads
ObjectiveLearn how to connect Threads.

Communicating Between Threads with Piped Streams

The java.io.PipedInputStream class and java.io.PipedOutputStream class provide a convenient means to move streaming data from one thread to another. Output from one thread becomes input for the other thread. Piped streams are normally created in pairs. The piped output stream becomes the underlying source for the piped input stream.
To use the piped output stream as the underlaying source for the piped input stream, you might write something like this:

PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);

This simple example is a little deceptive because these lines of code will normally be in different methods, and perhaps even different classes. Some mechanism must be established to pass a reference to the PipedOutputStream into the thread that handles the PipedInputStream. Or you can create them in the same thread; then pass a reference to the connected stream into a separate thread.
Alternatively, you can reverse this:
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);

Figure 5.9: Data moving between threads with piped streams

Or you can create them both unconnected, then use one or the other's connect() method to link them:
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
pis.connect(pos);

Otherwise, these classes just have the usual
  1. read(),
  2. write(),
  3. flush(),
  4. close(), and
  5. available()
methods like all stream classes.