参考:https://blog.csdn.net/qq_42857603/article/details/82120659
java中,PipedOutputStream和PipedInputStream分别是管道输出流和管道输入流
使用管道通信时,必须将PipedOutputStream和PipedInputStream配套使用
大致流程:
我们在线程A中向PipedOutputStream中写入数据,这些数据会自动的发送到与PipedOutputStream对应的PipedInputStream中,进而存储在PipedInputStream的缓冲中;
线程B通过读取PipedInputStream中的数据
字节管道流多线程通信代码演示:
package com.example.io.piped.bytePiped;
import java.io.IOException;
import java.io.PipedOutputStream;
import java.util.concurrent.TimeUnit;
public class WriteData {
private int count = 0;
public void writeMethod(PipedOutputStream pipedOutputStream) throws IOException, InterruptedException {
while (true){
// 每隔1s向输出流写入数字字符串
pipedOutputStream.write(String.valueOf(count++).getBytes());
// pipedOutputStream.write("123456".getBytes());
// System.out.println(Thread.currentThread().getName() + ": "+count);
TimeUnit.SECONDS.sleep(1);
}
}
}
package com.example.io.piped.bytePiped;
import java.io.IOException;
import java.io.PipedInputStream;
public class ReadData {
public void readMethod(PipedInputStream pipedInputStream) throws IOException {
byte[] bytes = new byte[1024];
int read;
// 当流不存在数据时候,read方法会进入阻塞状态
while ((read = pipedInputStream.read(bytes)) != -1){
String newData = new String(bytes,0,read);
System.out.println("Get Data= " + newData);
// System.out.println(Thread.currentThread().getName() + ": "+newData);
}
}
}
package com.example.io.piped.bytePiped;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class BytePipedTest01 {
public static void main(String[] args) {
// 创建读写对象
WriteData writeData = new WriteData();
ReadData readData = new ReadData();
// 创建管道输入输出流
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
try {
// 重点:连接管道流
pipedInputStream.connect(pipedOutputStream);
// 创建管道流
ThreadRead threadRead = new ThreadRead(readData, pipedInputStream);
ThreadWrite threadWrite = new ThreadWrite(writeData, pipedOutputStream);
threadRead.start();
threadWrite.start();
} catch (IOException e) {
e.printStackTrace();
}
}
static class ThreadRead extends Thread{
private ReadData readData;
private PipedInputStream pipedInputStream;
public ThreadRead(ReadData readData,PipedInputStream pipedInputStream){
this.readData = readData;
this.pipedInputStream = pipedInputStream;
}
@Override
public void run() {
try {
readData.readMethod(pipedInputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
static class ThreadWrite extends Thread{
private WriteData writeData;
private PipedOutputStream pipedOutputStream;
public ThreadWrite(WriteData writeData,PipedOutputStream pipedOutputStream){
this.writeData = writeData;
this.pipedOutputStream = pipedOutputStream;
}
@Override
public void run() {
try {
writeData.writeMethod(pipedOutputStream);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
字符管道流多线程通信代码演示:
package com.example.io.piped.charPiped;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
public class CharPipedTest01 {
public static void main(String[] args) {
PipedWriter pw = new PipedWriter();
try {
PipedReader pr = new PipedReader(pw);
Thread t1 = new ReadPipde("读", pr);
Thread t2 = new Writerpiped("写", pw);
t1.start();
t2.start();
} catch (IOException e) {
e.printStackTrace();
}
}
static class ReadPipde extends Thread{
private PipedReader pr;
public ReadPipde(String name,PipedReader pr){
super(name);
this.pr = pr;
}
@Override
public void run() {
try {
int len = 0;
char[] ch = new char[1024];
while ((len = pr.read(ch)) != -1){
// 字符数组不能直接打印,直接打印是地址
StringBuilder str = new StringBuilder();
for(char c1 : ch){
str.append(c1);
}
System.out.println(getName() + ":"+str.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
static class Writerpiped extends Thread{
private PipedWriter pw;
public Writerpiped(String name,PipedWriter pw){
super(name);
this.pw = pw;
}
@Override
public void run() {
char[] c = "123中国加油!!".toCharArray();
try {
while (true){
pw.write(c,0,c.length);
// 字符数组不能直接打印,直接打印是地址
StringBuilder str = new StringBuilder();
for(char c1 : c){
str.append(c1);
}
System.out.println(getName() + ":" + str.toString());
pw.flush();
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}