本帖最后由 吴亨 于 2011-12-17 12:30 编辑
import java.io.*;
class Sender extends Thread
{
public PipedOutputStream outs;
public Sender(PipedOutputStream outs)
{
this.outs = outs;
}
public void run()
{
try
{
outs.write("Hello,Receiver!".getBytes());
outs.close();
} catch(Exception ex){}
}
}
class Receiver extends Thread
{
public PipedInputStream inp;
public Receiver(PipedInputStream inp)
{
this.inp = inp;
try
{
inp.close();
} catch(Exception ex){}
}
public void run()
{try
{
byte[] buf = new byte[1024];
inp.read(buf);
System.out.println("following: "+new String(buf));
} catch(Exception ex){}
}
}
class TestPiped
{
public static void main(String[]args) throws Exception
{
PipedOutputStream outs = new PipedOutputStream();
Sender sen = new Sender(outs);
PipedInputStream inp = new PipedInputStream();
Receiver rec = new Receiver(inp);
outs.connect(inp);
sen.start();
rec.start();
}
}
Sender与Receiver之间没有建立通信吗? |
|