// COM3336 Operating Systems // Homework 1 // John Sung // Started: 10/8/1999 // import java.io.*; import ThreadMutex; // the class that will output the given input stream // to the given printstream. public class OutputInstreamThread extends Thread { /////////////////////////////////////////// // private variables // the input stream that we need to output private InputStream Instream; // the output stream in which we will be outputing to private PrintStream Outstream; // the mutex in which we need to take ourself on and off // to signal some waiting thread that we're done. private ThreadMutex StreamThreadCount; // constructor OutputInstreamThread(InputStream instream, PrintStream outstream, ThreadMutex mutex) { // set the stream references Instream = instream; Outstream = outstream; // set the mutex for the counter StreamThreadCount = mutex; // add myself to the count StreamThreadCount.changeThreadCount(1); } // the run function to do the actual work // of directing instream data to the outstream. public void run() { try { // attempt to read a byte from the instream int readByte = Instream.read(); // while we're successful, keep on printing // the byte to the PrintStream while (readByte != -1) { Outstream.print((char)readByte); readByte = Instream.read(); } } catch (IOException ex) { } // now that we're done, take outself out // from the thread count StreamThreadCount.changeThreadCount(-1); } }