package test;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class 管道流 {
public static void main(String[] args) throws IOException, InterruptedException {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
pis.connect(pos);
Input in = new Input(pis);
Output out = new Output(pos);
new Thread(in).start();
Thread.sleep(1000);
new Thread(out).start();
}
}
class Input implements Runnable{
private PipedInputStream pis;
public Input(PipedInputStream pis){
this.pis = pis;
}
@Override
public void run() {
byte[] bs = new byte[1024];
try {
int len = pis.read(bs);
String str = new String(bs, 0, len);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Output implements Runnable{
private PipedOutputStream pos;
public Output(PipedOutputStream pos){
this.pos = pos;
}
@Override
public void run() {
String str = "出出--------------------------";
try {
pos.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}