import java.io.*;
public class PipedStream
{
public static void main(String args[])
{
try
{
//Thread t1=new Sender();
Thread t2=new Receiver();
//PipedOutputStream out=t1.getOutputStream();
PipedInputStream in=t2.getInputStream();//这个地方报的错,报错为无法找到getInputStream().此处为了方便我把线程t1关闭了,并且在打开t1比调用它的情
况下报的错与t2的一样。
//out.connect(in);
//t1.start();
t2.start();//注:当我把不开启t2线程时,报的错也是一样的,也就是在单线程的情况下也不能够用in去调用getInputStream()方法,什么原因呀。谢谢.
}
catch(IOException ee)
{
System.out.println(ee.getMessage());
}
}
}
class Sender extends Thread
{
private PipedOutputStream out=new PipedOutputStream();
public PipedOutputStream getOutputStream()
{
return out;
}
public void run()
{
String s=new String("hell heima");
try
{
out.write(s.getBytes());
out.close();
}
catch(IOException ee)
{
System.out.println(ee.getMessage());
}
}
}
class Receiver extends Thread
{
private PipedInputStream in=new PipedInputStream();
public PipedInputStream getInputStream()
{
return in;
System.out.println("haha");
}
public void run()
{
String s1=null;
byte[] buf=new byte[1024];
try
{
int len=in.read(buf);
s1=new String(buf,0,len);
System.out.println("the following message comes from sender:\n+s");
in.close();
}
catch(IOException e)
{
System.out.println(e.getMessage()+"aa");
}
}
} |