スレッド間の通信:パイプ

スレッド間の通信:パイプ

  1. パイプラインの入力および出力バイトストリームは、PipedOutputStream(パイプラインへの出力)、PipedInputStream(パイプラインへのデータ入力)を使用します
  2. パイプ内の入力および出力文字ストリーム。PipedReader(パイプから読み取る)、PipedWriter(パイプに書き込む)を使用します。

バイトストリームの入出力をパイプします。

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();
    }
}

パイプでの文字ストリームの入力と出力

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();
    }
}

おすすめ

転載: blog.csdn.net/weixin_43960044/article/details/121083361