import java.io.*;
class Read implements Runnable
{
private PipedInputStream in;
Read(PipedInputStream in)
{
this.in=in;
}
public void run()
{
try
{
byte[] b=new byte[1024];
int len=0;
while((len=in.read())!=-1)
{
String s=new String(b,0,len);
System.out.println(s);
in.close();
}
}
catch(IOException e)
{
throw new RuntimeException("管道堵塞,读取失败");
}
}
}
class Writer implements Runnable
{
private PipedOutputStream out;
Writer(PipedOutputStream out)
{
this.out=out;
}
public void run()
{
try
{
out.write("飞吧".getBytes());
out.close();
}
catch(IOException e)
{
throw new RuntimeException("管道堵塞,写入失败");
}
}
}
class TestDemo
{
public static void main(String[]args)throws IOException
{
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream();
in.connect(out);
Read r=new Read(in);
Writer w=new Writer(out);
Thread t=new Thread(r);
Thread t1=new Thread(w);
t.start();
t1.start();
}
}
程序为什么编译通过了,但运行失败了呢??求解决。
|
|