管道流(pipeStream)是一种特殊的流,用于线程间的通信。
字节流:PipedInputStream和PipedOutputStream
字符流:PipedReader和PipedWriter
字节流
package com.myObject;
import java.io.IOException;
import java.io.PipedInputStream;
public class ReadObj {
public void readMothd(PipedInputStream is) {
try {
System.out.println("read begin");
byte[] byteArr = new byte[20];
int readLength;
while ((readLength = is.read(byteArr)) != -1) {
String str = new String(byteArr,0,readLength);
System.out.println(str);
}
System.out.println("red end");
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.myObject;
import java.io.IOException;
import java.io.PipedOutputStream;
public class WriteObj {
public void writeMothd(PipedOutputStream os) {
try {
System.out.println("write begin");
for(int i = 0; i < 50; i++){
String str = ""+(1+i);
os.write(str.getBytes());
}
System.out.println("write end");
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.myThread;
import java.io.PipedInputStream;
import com.myObject.ReadObj;
public class Thread1a extends Thread{