Communication between threads: Piped

Communication between threads: Piped

  1. Input and output byte streams in the pipeline use PipedOutputStream (output to the pipeline), PipedInputStream (data input in the pipeline)
  2. Input and output character streams in the pipe, use PipedReader (read from the pipe), PipedWriter (write to the pipe)

Pipe byte stream input and output:

package com.chapter03;

import java.io.*;

class WriteData {
    
    
    public void write(PipedOutputStream out) {
    
    
        try {
    
    
            for (int i = 0; i < 300; i++) {
    
    
                String s = " " + (i + 1);
                out.write(s.getBytes());
                System.out.println("write: " + s);
            }
            out.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

class ReadData {
    
    
    public void read(PipedInputStream in) {
    
    
        try {
    
    
            // int 为4个字节 20字节为4个数字
            byte[] arr = new byte[20];

            int length = in.read(arr);
            while (length != -1) {
    
    
                String s = new String(arr, 0, length);
                System.out.println("\nread: " + s);
                length = in.read(arr);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

public class StudyThreads05管道线程通信_读取字节流 {
    
    
    public static void main(String[] args) throws IOException, InterruptedException {
    
    
        PipedOutputStream out = new PipedOutputStream();
        PipedInputStream in = new PipedInputStream();
        out.connect(in);
        Thread threadWrite = new Thread() {
    
    
            @Override
            public void run() {
    
    
                new WriteData().write(out);
            }
        };

        Thread threadRead = new Thread() {
    
    
            @Override
            public void run() {
    
    
                new ReadData().read(in);
            }
        };

        threadRead.start();
        Thread.sleep(2000);
        threadWrite.start();
    }
}

Character stream input and output in pipes

package com.chapter03;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

class WriteData06 {
    
    
    public void write(PipedWriter writer) {
    
    
        try {
    
    
            for (int i = 0; i < 300; i++) {
    
    
                String s = " " + (i + 1);
                writer.write(s.toCharArray());
                System.out.println("write: " + s);
//                Thread.sleep((long) (Math.random() * 10));
            }
            writer.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

class ReadData06 {
    
    
    public void read(PipedReader reader) {
    
    
        try {
    
    
            // 读取20个字符
            char[] arr = new char[20];
            int length = reader.read(arr);
            while (length != -1) {
    
    
                String s = new String(arr, 0, length);
                System.out.println("read: " + s);
                length = reader.read(arr);
//                Thread.sleep((long) (Math.random() * 100));
            }
            reader.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

public class StudyThreads06管道线程通信_读写字符流 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        PipedWriter writer = new PipedWriter();
        PipedReader reader = new PipedReader();
        writer.connect(reader);

        Thread threadWrite = new Thread(() -> new WriteData06().write(writer));
        Thread threadRead = new Thread(() -> new ReadData06().read(reader));

        threadRead.start();
        threadWrite.start();
    }
}

Guess you like

Origin blog.csdn.net/weixin_43960044/article/details/121083361