java thread communication - pipe pipeline

Keywords: Java

There are many ways of communication between java threads. Here we summarize our understanding of pipe pipeline communication.

1. Establishing the connection between the input end and the output end of the pipeline
First, in order to create a pipeline flow, we must first create a PipedOutputStream object, and then create a PipedInputStream object. As follows:
PipedOutputStream out = null;
PipedInputStream in = null;
After the object is established, connect() method is used to establish the connection between the two.
out.connect(in);
This method is available in PipedOutputStream and PipedInputStream, which can be called to establish a connection at will, but pay attention to the intelligent establishment of a connection, repeated establishment will throw an exception.
Connections can also be established without using the connect() method, as follows:
out = new PipedOutputStream(in );
Once a pipeline is established, data can be read and written to the pipeline like operation files.

2. Starting Communications
First of all, we should pay special attention to the fact that we can't write and read in the same thread, which will cause deadlock, because the pipeline will be blocked (when there is no data in the pipeline, the thread of reading operation will block until there are threads to write data; when the pipeline is full of data, the thread of writing operation will block until there are threads to read data). According to this, sometimes both ends of writing and reading are working at the same time. There is only one thread to complete reading and writing. Obviously, there is no guarantee of simultaneous reading and writing, so reading and writing should be done in a separate thread.
The pipeline is a 1024 byte circular buffer array. The data read from the pipeline will be cleared out of the pipeline. That is to say, after reading, it is equivalent to taking the data away from the pipeline, so it is a circular buffer array.
The following demo code:

package cn.zhoucy.pipe;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class TestPiped {

public static void main(String[] args) {

    Sender sender = new Sender();
    Recive recive = new Recive();
    PipedInputStream pi = recive.getPipedInputputStream();
    PipedOutputStream po = sender.getPipedOutputStream();
    try {
        pi.connect(po);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    new Thread(sender).start();
    new Thread(recive).start();

}
}

class Sender implements Runnable {
PipedOutputStream out = null;

public PipedOutputStream getPipedOutputStream() {
    out = new PipedOutputStream();
    return out;
}

@Override
public void run() {

    try {
        out.write("Hello , Reciver!".getBytes());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    try {
        out.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

}

}

class Recive implements Runnable {
PipedInputStream in = null;

public PipedInputStream getPipedInputputStream() {
    in = new PipedInputStream();
    return in;
}

@Override
public void run() {

    byte[] bys = new byte[1024];
    try {
        in.read(bys);
        System.out.println("Read the information:" + new String(bys).trim());
        in.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

    }
}

The results are as follows:

Reference article:
http://blog.csdn.net/zlp1992/article/details/50298195#comments
http://www.cnblogs.com/songxingzhu/archive/2012/09/17/2688969.html

Posted by nobodyk on Sat, 23 Mar 2019 07:09:55 -0700