package com.huyd.day20;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
/**
* 线程管道流
*
* */
public class PipedStreamDemo {
public static void main(String[] args) {
PipedInputStream in =new PipedInputStream();
PipedOutputStream out =new PipedOutputStream();
try {
in.connect(out);
} catch (IOException e) {
e.printStackTrace();
}
Read r=new Read(in);
Write w=new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
class Read implements Runnable{
private PipedInputStream in;
Read (PipedInputStream in){
this.in=in;
}
@Override
public void run() {
try {
byte [] b=new byte[1024];
int i=in.read(b);
String str =new String(b, 0, i);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Write implements Runnable{
private PipedOutputStream out;
Write(PipedOutputStream out){
this.out=out;
}
@Override
public void run() {
try {
out.write("run fus....".getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
/**
* 线程管道流
*
* */
public class PipedStreamDemo {
public static void main(String[] args) {
PipedInputStream in =new PipedInputStream();
PipedOutputStream out =new PipedOutputStream();
try {
in.connect(out);
} catch (IOException e) {
e.printStackTrace();
}
Read r=new Read(in);
Write w=new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
class Read implements Runnable{
private PipedInputStream in;
Read (PipedInputStream in){
this.in=in;
}
@Override
public void run() {
try {
byte [] b=new byte[1024];
int i=in.read(b);
String str =new String(b, 0, i);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Write implements Runnable{
private PipedOutputStream out;
Write(PipedOutputStream out){
this.out=out;
}
@Override
public void run() {
try {
out.write("run fus....".getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}