Java Reference
In-Depth Information
public void connect(PipedInputStream dest). Connects the given pipe
input stream to the pipe output stream that this method is invoked on.
public void connect(PipedOutputStream dest). Connects the given pipe
output stream to the pipe input stream that this method is invoked on.
The PipedReader and PipedWriter classes work in a similar fashion. Their
constructors look similar to:
public PipedReader(). Creates a new, unconnected pipe reader.
public PipedReader(PipedWriter).
Creates a new pipe reader that is con-
nected to the given pipe writer.
public PipedWriter(). Creates a new, unconnected pipe writer.
public PipedWriter(PipedReader).
Creates a new pipe writer that is con-
nected to the given pipe reader.
As with the pipe streams, if the PipedReader and PipedWriter are not con-
nected using the constructors, they can be connected using the connect()
method.
The following PipeDemo program demonstrates two threads communicating
with each other, using the PipedInputStream and PipedOutputStream classes.
The data being sent back and forth is an int and a String object, so the pipes are
chained with a DataInputStream and DataOutputStream, respectively.
The data being sent is generated from the following RandomWeather class:
import java.io.*;
import java.util.*;
public class RandomWeather extends TimerTask
{
private DataOutputStream out;
public RandomWeather(OutputStream dest)
{
out = new DataOutputStream(dest);
}
public void run()
{
try
{
int temp = (int) (Math.random() * 110);
out.writeInt(temp);
int random = (int) (Math.random() * 4);
String conditions;
switch(random)
{
case 0:
Search WWH ::




Custom Search