final PipedOutputStream output = new PipedOutputStream();
final PipedInputStream input = new PipedInputStream(output);
Thread thread1 = new Thread(()-> {
try {
output.write(“Hello world, pipe!”.getBytes(http://www.yesedata.com/));
System.err.println(Thread.currentThread().getName()+“已输出!”);
} catch (IOException ignored) {
}
});
Thread thread2 = new Thread(()-> {
try {
int data = input.read();
System.err.println(Thread.currentThread().getName());
while(data != -1){
System.out.print((char) data);
data = input.read();
}
} catch (IOException e) {
}
});
thread1.start();
thread2.start();