(IO in-depth operation) pipeline flow

Pipeline flow

The function of the pipeline flow is to implement IO operations between two threads.

 The pipeline flow is also divided into two categories:

  • Byte pipe stream:
  • PipedInputStream :

Connection processing: public void connect​(PipedOutputStream src) throws IOException

  • PipedOutputStream:
  • Character pipe stream:
  • PipedWriter

Connection processing: public void connect​(PipedOutput src) throws IOException

  • PopedWriter

Example: Implement pipeline operation

package IO深入操作;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class 管道流 {
    public static void main(String[] args) {
        SendThread sendThread = new SendThread();
        ReceiveThread receiveThread = new ReceiveThread();
        try {
            sendThread.getOutput().connect(receiveThread.getInput());   //进行管道连接
        } catch (IOException e) {
            e.printStackTrace();
        }
        new Thread(sendThread,"消息发送线程").start();
        new Thread(receiveThread,"消息接收线程").start();
    }
}

class SendThread implements Runnable{
    private PipedOutputStream output;    //管道输出流

    public SendThread(){
        this.output = new PipedOutputStream();  //实例化管道输出流
    }
    @Override
    public void run() {
        for(int x = 0;x < 10;x++){
            try {   //利用管道实现数据发送处理
                this.output.write((Thread.currentThread().getName()+"第"+(x+1)+"次信息\n").getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            this.output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public PipedOutputStream getOutput(){
        return this.output;
    }
}

class ReceiveThread implements Runnable{
    private PipedInputStream input;

    public ReceiveThread(){
        this.input = new PipedInputStream();
    }
    @Override
    public void run(){
        byte[] data = new byte[1024];
            try {
                int len = this.input.read(data);    //保存数据到data并记录长度
                System.out.println(Thread.currentThread().getName()+"接收"+new String(data,0,len));   //输出data
            } catch (IOException e) {
                e.printStackTrace();
            }
        try {
            this.input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public PipedInputStream getInput(){
        return this.input;
    }
}

The message receiving thread receives the first
message of the message sending thread The second message of the
message sending thread The third message of the
message sending thread The fourth message of the
message sending thread The fifth message of the
message sending thread The sixth message of the message sending thread The sixth message of the message sending thread The sixth message of the
message sending thread 7 times information
8th information of
message sending thread 9th information of
message sending thread 10th information of message sending thread

The pipeline is similar to the effect of the hospital. One is only responsible for sending, the other is only responsible for receiving, and the middle is connected by the pipeline.

Guess you like

Origin blog.csdn.net/weixin_46245201/article/details/112916079