java-io流--(七)--管道io进行多线程通信

参考:https://blog.csdn.net/qq_42857603/article/details/82120659
java中,PipedOutputStream和PipedInputStream分别是管道输出流和管道输入流
使用管道通信时,必须将PipedOutputStream和PipedInputStream配套使用
大致流程:

我们在线程A中向PipedOutputStream中写入数据,这些数据会自动的发送到与PipedOutputStream对应的PipedInputStream中,进而存储在PipedInputStream的缓冲中;
线程B通过读取PipedInputStream中的数据

在这里插入图片描述
字节管道流多线程通信代码演示:

package com.example.io.piped.bytePiped;

import java.io.IOException;
import java.io.PipedOutputStream;
import java.util.concurrent.TimeUnit;

public class WriteData {

    private int count = 0;

    public void writeMethod(PipedOutputStream pipedOutputStream) throws IOException, InterruptedException {

        while (true){
//            每隔1s向输出流写入数字字符串
            pipedOutputStream.write(String.valueOf(count++).getBytes());
//            pipedOutputStream.write("123456".getBytes());
//            System.out.println(Thread.currentThread().getName() + ": "+count);
            TimeUnit.SECONDS.sleep(1);
        }

    }

}
package com.example.io.piped.bytePiped;

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

public class ReadData {

    public void readMethod(PipedInputStream pipedInputStream) throws IOException {
        byte[] bytes = new byte[1024];
        int read;
//        当流不存在数据时候,read方法会进入阻塞状态
        while ((read = pipedInputStream.read(bytes)) != -1){
            String newData = new String(bytes,0,read);
            System.out.println("Get Data= " + newData);
//            System.out.println(Thread.currentThread().getName() + ": "+newData);
        }
    }

}
package com.example.io.piped.bytePiped;

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

public class BytePipedTest01 {

    public static void main(String[] args) {

//        创建读写对象
        WriteData writeData = new WriteData();
        ReadData readData = new ReadData();

//        创建管道输入输出流
        PipedInputStream pipedInputStream = new PipedInputStream();
        PipedOutputStream pipedOutputStream = new PipedOutputStream();


        try {
            //        重点:连接管道流
            pipedInputStream.connect(pipedOutputStream);

//            创建管道流
            ThreadRead threadRead = new ThreadRead(readData, pipedInputStream);
            ThreadWrite threadWrite = new ThreadWrite(writeData, pipedOutputStream);
            threadRead.start();
            threadWrite.start();


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    static class ThreadRead extends Thread{

        private ReadData readData;
        private PipedInputStream pipedInputStream;
        public ThreadRead(ReadData readData,PipedInputStream pipedInputStream){
            this.readData = readData;
            this.pipedInputStream = pipedInputStream;
        }

        @Override
        public void run() {
            try {
                readData.readMethod(pipedInputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static class ThreadWrite extends Thread{
        private WriteData writeData;
        private PipedOutputStream pipedOutputStream;

        public ThreadWrite(WriteData writeData,PipedOutputStream pipedOutputStream){
            this.writeData = writeData;
            this.pipedOutputStream = pipedOutputStream;
        }

        @Override
        public void run() {
            try {
                writeData.writeMethod(pipedOutputStream);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}

在这里插入图片描述

字符管道流多线程通信代码演示:

package com.example.io.piped.charPiped;

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

public class CharPipedTest01 {

    public static void main(String[] args) {

        PipedWriter pw = new PipedWriter();

        try {
            PipedReader pr = new PipedReader(pw);
            Thread t1 = new ReadPipde("读", pr);
            Thread t2 = new Writerpiped("写", pw);
            t1.start();
            t2.start();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    static class ReadPipde extends  Thread{

        private PipedReader pr;

        public ReadPipde(String name,PipedReader pr){
            super(name);
            this.pr = pr;
        }

        @Override
        public void run() {
                try {
                    int len = 0;
                    char[] ch = new char[1024];
                    while ((len = pr.read(ch)) != -1){

                        //                        字符数组不能直接打印,直接打印是地址
                        StringBuilder str = new StringBuilder();
                        for(char c1 : ch){
                            str.append(c1);

                        }
                        System.out.println(getName() + ":"+str.toString());

                    }



                } catch (IOException e) {
                    e.printStackTrace();
                }

        }
    }

    static class Writerpiped extends Thread{

        private PipedWriter pw;
        public Writerpiped(String name,PipedWriter pw){
            super(name);
            this.pw = pw;
        }

        @Override
        public void run() {

            char[] c = "123中国加油!!".toCharArray();


                try {
                    while (true){

                        pw.write(c,0,c.length);
                        //                        字符数组不能直接打印,直接打印是地址
                        StringBuilder str = new StringBuilder();
                        for(char c1 : c){
                            str.append(c1);
                        }
                        System.out.println(getName() + ":" + str.toString());
                        pw.flush();

                        Thread.sleep(1000);

                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }




        }
    }



}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值