美文网首页
Java 通过管道进行线程间通信

Java 通过管道进行线程间通信

作者: 杰哥长得帅 | 来源:发表于2018-03-16 20:04 被阅读32次

    Java 中的管道流(pipeStream)可以在不同线程间直接传送数据。一个线程发送数据到输出管道,另一个线程从输入管道中读数据。通过使用管道,实现不同线程间的通信,无须借助于类似临时文件的东西

    在 Java 的 JDK 中提供了 4 个类来使线程间可以进行通信:

    1. PipedInputStream和PipedOutputStream
    2. PipedReader和PipedWriter

    通过管道进行线程间通信:字节流/字符流

    public class WriteData {
        public void writeMethod(PipedOutputStream out) {   // 字符流这里换成 PipeWriter
            try {
                System.out.println("write :");
                for (int i=0; i<10; i++) {
                    String outData = "" + (i+1);
                    out.write(outData.getBytes());    // 字符流这里换成 out.write(outData)
                    System.out.print(outData);
                }
                System.out.println();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public class ReadData {
        public void readMethod(PipedInputStream input) {    // 字符流这里换成 PipedReader
            try {
                System.out.println("read: ");
                byte[] byteArray = new byte[20];    // 字符流这里换成 char
                int readLength = input.read(byteArray);  
                while (readLength != -1) {
                    String newData = new String(byteArray, 0, readLength);
                    System.out.println(newData);
                    readLength = input.read(byteArray);
                }
                System.out.println();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    两个自定义线程

    public class ThreadWrite extends Thread {
        private WriteData write;
        private PipedOutputStream out;    // 字符流这里换成 PipedWriter
    
        public ThreadWrite(WriteData write, PipedOutputStream out) {    // 字符流这里换成 PipedWriter
            this.write = write;
            this.out = out;
        }
        public void run() {
            write.writeMethod(out);
        }
    }
    
    public class ThreadRead extends Thread {
        private ReadData read;
        private PipedInputStream input;    // 字符流这里换成 PipedRead
    
        public ThreadRead(ReadData read, PipedInputStream input) {   // 字符流这里换成 PipedRead
            this.read = read;
            this.input = input;
        }
        public void run() {
            read.readMethod(input);
        }
    }
    

    主类:

    public class Run {
        public static void main(String[] args) throws InterruptedException, IOException {
            WriteData writeData = new WriteData();
            ReadData readData = new ReadData();
    
            PipedInputStream inputStream = new PipedInputStream();   // 字符流用 PipedReader
            PipedOutputStream outputStream = new PipedOutputStream();  // 字符流用 PipedWriter
    
            // 使两个 Stream 之间产生通信链接
            inputStream.connect(outputStream);   // 这两个只能用一个
            //outputStream.connect(inputStream);
    
            ThreadRead threadRead = new ThreadRead(readData,inputStream);
            threadRead.start();
    
            Thread.sleep(2000);
    
            ThreadWrite threadWrite = new ThreadWrite(writeData,outputStream);
            threadWrite.start();
        }
    }
    

    结果:

    read:
    write :
    12345678910
    12345678910

    相关文章

      网友评论

          本文标题:Java 通过管道进行线程间通信

          本文链接:https://www.haomeiwen.com/subject/zyvrqftx.html