管道流与线程联系实例:
import java.io.*;
public class Piped {
public static void main(String[] args)throws Exception
{
PipedOutputStream o=new PipedOutputStream();
PipedInputStream i=new PipedInputStream();
o.connect(i);
new producer(o).start();
new comsumer(i).start();
}
}
class producer extends Thread{
PipedOutputStream o;
producer(PipedOutputStream o)
{
this.o=o;
}
public void run()
{
try
{
o.write("I love you".getBytes());
o.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class comsumer extends Thread{
PipedInputStream i;
comsumer(PipedInputStream i)
{
this.i=i;
}
public void run()
{
try
{
byte[] b=new byte[100];
int len=i.read(b);
System.out.println(new String(b,0,len));
i.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}