An Example
Home ] Up ]

 

 

Let's modify the Producer/Consumer example we saw before to use Piped classes for coordination:

package threads;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintWriter;

/**
 *  This is a PipedProducer thread that supplies strings
 *  to a PipedConsumer thread.  It reads a line from System.in
 *  and passes it to the PipedConsumer through a SharedBuffer,
 *  until the end of stream occurs.
 */
public class PipedProducer extends Thread
{
  public static void main(String[] args)
  {
    try
    {
      // Create the input and output pipes and hook them up
      PipedWriter writer = new PipedWriter();
      PipedReader reader = new PipedReader(writer);
      // Create and start a consumer thread
      PipedConsumer consumer = new PipedConsumer(reader);
      consumer.start();
      // Create and start a PipedProducer thread
      PipedProducer producer = new PipedProducer(writer);
      producer.start();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
  
  PipedProducer(PipedWriter writer)
  {
    super("Producer");
    m_writer = new PrintWriter(writer);
  }
  
  public void run()
  {
    // Connect up to System.in
    BufferedReader reader = new BufferedReader(
      new InputStreamReader(System.in)
      );
    
    try
    {
      // Loop, asking for input from the user until EOF
      while (true)
      {
        String line = reader.readLine();
        if (line == null)
          break;      // End of input
        m_writer.println(line);
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    
    System.out.println("Done with input: Closing pipe");
    m_writer.close();
  }
  
  /// Private data ///
  private PrintWriter m_writer;
}

/**
 *  This is a PipedConsumer thread that waits for input from
 *  the PipedProducer, supplied through a SharedBuffer.  It
 *  accepts the string passed, reverses it, and prints out
 *  the result.  When it encounters a null string, the thread
 *  exits.
 */
class PipedConsumer extends Thread
{
  PipedConsumer(PipedReader reader)
  {
    super("Consumer");
    m_reader = new BufferedReader(reader);
  }
  
  public void run()
  {
    try
    {
      while (true)
      {
        String line = m_reader.readLine();
        if (line == null)
          break;
        // Reverse the line and print it out
        line = reverse(line);
        System.out.println(line);
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    
    System.out.println("Consumer thread exiting...");
  }
  
  private String reverse(String string)
  {
    StringBuffer buffer = new StringBuffer(string);
    return buffer.reverse().toString();
  }
  
  //// Private data ////
  private BufferedReader m_reader;
}
 

Here's an example of the output from one run of this program:

Hello there!
!ereht olleH
Nice to see you!
!uoy ees ot eciN
Gotta go!
!og attoG
Done with input: Closing pipe
Consumer thread exiting...
 
The page was last updated February 19, 2008