Threading Piped Streams

Piped streams are typically used in separate threads. One thread writes output to a PipedOutputStream. The other thread reads the same data from a PipedInputStream. The two streams are connected either through their constructors or through an explicit connect() method. To find out how to instantiate piped streams, see the tips “Connecting the Piped Stream Classes” and “Using Piped Streams in an Application.”

You can use the piped streams in two dependent threads in a single class. This class creates two piped streams and connects them through the constructor in Lines 1-2. Lines 4-5 construct two threads with these streams as a parameter to each of them. The thread inThread will read in the data from the pins and the thread outThread will write the data to pouts. Lines 7-8 start the threads.

 1.      private PipedInputStream pins = new PipedInputStream();2.      private PipedOutputStream pouts = new PipedOutputStream(pins);3.4.    Thread inThread = new PipedInputThread(pins);5.    Thread outThread = new PipedOutputThread(pouts);6.7.    inThread.start();8.    outThread.start();

The code for the run() methods for the two threads is given in the tip “Running Piped Threads.” However, the classes are defined here. The class PipedInputThread is as follows:

 1. class PipedInputThread extends Thread {2.   InputStream in_ = null;3.4.   public PipedInputThread (InputStream in) { in_ = in; }5.   public void run () {6.     // Code that reads in from the PipedInputStream "in_"7.   }8. }

The code for the class PipedOutputThread is similar:

 1. class PipedOutputThread extends Thread {2.      OutputStream out_ = null;3.4.      public PipedOutputThread (OutputStream out) { out_ = out; }5.      public void run ()6.        // Code that writes out to the PipedOutputStream "out_"7.      }8. }
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may