import java.io.*;
class Read implements Runnable
{
private PipedInputStream in;
Read(PipedInputStream in)
{
this.in = in ;
}
public void run()
{
try
{
byte []buf = new byte[1024];
int len = in.read(buf);
String s = new String(buf,0,len);
System.out.println(s);
in.close();
}
catch(IOException e){throw new RuntimeException("失败");}
}
}
class Write implements Runnable
{
private PipedOutputStream out;
Write(PipedOutputStream out)
{
this.out = out ;
}
public void run()
{
try
{
Thread.sleep(6000);
out.write("guandaolaile...".getBytes());
out.close();
}
catch(IOException e){throw new RuntimeException("失败");} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class PipedStreamDemo
{
public static void main(String []args)throws Exception
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
Read r =new Read(in);
Write w = new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
刚写了这个,但是管道流失败,怎么办? |