多线程(六)~通过管道进行线程之间的通信:字节流、字符流

学习文件IO等操作的时候用到了流,例如文件上传的时候是将文件转换成二进制的流传到服务器端。

在不同线程之间通信也是用到了对管道流的操作,在java中提供了4个类来操作管道流(pipeStream):
    1).PipedInputStream和PipedOutputStream
    2).PipedReader和PipedWriter
字节流Demo:
①.写数据、读数据
  1. package com.multiThread.bean;
  2. import java.io.PipedOutputStream;
  3. publicclassWriteData{
  4. publicvoid writeMethod(PipedOutputStream outputStream){
  5. try{
  6. System.out.println("write:");
  7. for(int i =0;i<100;i++){
  8. String outData =""+(i +1);
  9. outputStream.write(outData.getBytes());
  10. System.out.print(outData);
  11. }
  12. System.out.println();
  13. outputStream.close();
  14. }catch(Exception e){
  15. e.printStackTrace();
  16. }
  17. }
  18. }
  1. package com.multiThread.bean;
  2. import java.io.PipedInputStream;
  3. publicclassReadData{
  4. publicvoid readMethod(PipedInputStream pipedInputStream){
  5. try{
  6. System.out.println("read:");
  7. byte[] b =newbyte[20];
  8. int readLength = pipedInputStream.read(b);
  9. while(readLength !=-1){
  10. String newData =newString(b,0,readLength);
  11. System.out.print(newData);
  12. readLength = pipedInputStream.read(b);
  13. }
  14. System.out.println();
  15. pipedInputStream.close();
  16. }catch(Exception e){
  17. e.printStackTrace();
  18. }
  19. }
  20. }
②.写数据线程、读数据线程
  1. package com.multiThread.thread;
  2. import java.io.PipedOutputStream;
  3. import com.multiThread.bean.WriteData;
  4. publicclassWriteThreadimplementsRunnable{
  5. privatePipedOutputStream pipedOutputStream;
  6. privateWriteData writeData;
  7. publicWriteThread(PipedOutputStream pipedOutputStream,WriteData writeData){
  8. this.pipedOutputStream = pipedOutputStream;
  9. this.writeData = writeData;
  10. }
  11. @Override
  12. publicvoid run(){
  13. writeData.writeMethod(pipedOutputStream);
  14. }
  15. }
  1. package com.multiThread.thread;
  2. import java.io.PipedInputStream;
  3. import com.multiThread.bean.ReadData;
  4. publicclassReadThreadimplementsRunnable{
  5. privatePipedInputStream pipedInputStream;
  6. privateReadData readData;
  7. publicReadThread(PipedInputStream inputStream,ReadData readData){
  8. this.pipedInputStream = inputStream;
  9. this.readData = readData;
  10. }
  11. @Override
  12. publicvoid run(){
  13. readData.readMethod(pipedInputStream);
  14. }
  15. }
③.测试
  1. package com.multiThread.test;
  2. import java.io.PipedInputStream;
  3. import java.io.PipedOutputStream;
  4. import com.multiThread.bean.ReadData;
  5. import com.multiThread.bean.WriteData;
  6. import com.multiThread.thread.ReadThread;
  7. import com.multiThread.thread.WriteThread;
  8. publicclassPipeTest{
  9. publicstaticvoid main(String[] args){
  10. try{
  11. WriteData writeData =newWriteData();
  12. ReadData readData =newReadData();
  13. PipedInputStream inputStream =newPipedInputStream();
  14. PipedOutputStream outputStream =newPipedOutputStream();
  15. inputStream.connect(outputStream);
  16. // outputStream.connect(inputStream);
  17. ReadThread readThread =newReadThread(inputStream, readData);
  18. Thread t2 =newThread(readThread);
  19. t2.start();
  20. Thread.sleep(2000);
  21. WriteThread writeThread =newWriteThread(outputStream, writeData);
  22. Thread t1 =newThread(writeThread);
  23. t1.start();
  24. }catch(Exception e){
  25. e.printStackTrace();
  26. }
  27. }
  28. }
执行结果:
  1. read:
  2. write:
  3. 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  4. 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
 
除了支持字节流之外,还可以使用字符流
只需要把读数据写数据的操作由“PipedInputStream和PipedOutputStream”换成“PipedReader和PipedWriter”即可,其他地方实现逻辑不变。这块就不做例子了。
 





posted @ 2017-02-27 14:13  鱼我所欲也  阅读(569)  评论(0编辑  收藏  举报