多线程(六)~通过管道进行线程之间的通信:字节流、字符流
学习文件IO等操作的时候用到了流,例如文件上传的时候是将文件转换成二进制的流传到服务器端。
在不同线程之间通信也是用到了对管道流的操作,在java中提供了4个类来操作管道流(pipeStream):
1).PipedInputStream和PipedOutputStream
2).PipedReader和PipedWriter
字节流Demo:
①.写数据、读数据
package com.multiThread.bean;
import java.io.PipedOutputStream;
publicclassWriteData{
publicvoid writeMethod(PipedOutputStream outputStream){
try{
System.out.println("write:");
for(int i =0;i<100;i++){
String outData =""+(i +1);
outputStream.write(outData.getBytes());
System.out.print(outData);
}
System.out.println();
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
package com.multiThread.bean;
import java.io.PipedInputStream;
publicclassReadData{
publicvoid readMethod(PipedInputStream pipedInputStream){
try{
System.out.println("read:");
byte[] b =newbyte[20];
int readLength = pipedInputStream.read(b);
while(readLength !=-1){
String newData =newString(b,0,readLength);
System.out.print(newData);
readLength = pipedInputStream.read(b);
}
System.out.println();
pipedInputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
②.写数据线程、读数据线程
package com.multiThread.thread;
import java.io.PipedOutputStream;
import com.multiThread.bean.WriteData;
publicclassWriteThreadimplementsRunnable{
privatePipedOutputStream pipedOutputStream;
privateWriteData writeData;
publicWriteThread(PipedOutputStream pipedOutputStream,WriteData writeData){
this.pipedOutputStream = pipedOutputStream;
this.writeData = writeData;
}
@Override
publicvoid run(){
writeData.writeMethod(pipedOutputStream);
}
}
package com.multiThread.thread;
import java.io.PipedInputStream;
import com.multiThread.bean.ReadData;
publicclassReadThreadimplementsRunnable{
privatePipedInputStream pipedInputStream;
privateReadData readData;
publicReadThread(PipedInputStream inputStream,ReadData readData){
this.pipedInputStream = inputStream;
this.readData = readData;
}
@Override
publicvoid run(){
readData.readMethod(pipedInputStream);
}
}
③.测试
package com.multiThread.test;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import com.multiThread.bean.ReadData;
import com.multiThread.bean.WriteData;
import com.multiThread.thread.ReadThread;
import com.multiThread.thread.WriteThread;
publicclassPipeTest{
publicstaticvoid main(String[] args){
try{
WriteData writeData =newWriteData();
ReadData readData =newReadData();
PipedInputStream inputStream =newPipedInputStream();
PipedOutputStream outputStream =newPipedOutputStream();
inputStream.connect(outputStream);
// outputStream.connect(inputStream);
ReadThread readThread =newReadThread(inputStream, readData);
Thread t2 =newThread(readThread);
t2.start();
Thread.sleep(2000);
WriteThread writeThread =newWriteThread(outputStream, writeData);
Thread t1 =newThread(writeThread);
t1.start();
}catch(Exception e){
e.printStackTrace();
}
}
}
执行结果:
read:
write:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
除了支持字节流之外,还可以使用字符流
只需要把读数据写数据的操作由“PipedInputStream和PipedOutputStream”换成“PipedReader和PipedWriter”即可,其他地方实现逻辑不变。这块就不做例子了。