线程之间通信:管道 (Piped)

线程之间通信:管道 (Piped)

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值