首页 > 代码库 > java多线程通过管道流实现不同线程之间的通信

java多线程通过管道流实现不同线程之间的通信

java中的管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据。一个线程发送数据到输出管道,另外一个线程从输入管道中读取数据。通过使用管道,实现不同线程间的通信,而不必借助类似临时文件之类的东西。jdk提供4个类来使线程建可以进行通信。

(1)PipedInputStream与PipedOutputStream

(2)PipedReader与PipedWriter

 

(1)PipedInputStream与PipedOutputStream

package com.ming.thread.pipeinputoutput;import java.io.PipedInputStream;public class ReadData {    public void readMethod(PipedInputStream input){        try {            System.out.println("read :");            byte[] byteArray=new byte[20];            int readLength=input.read(byteArray);            while(readLength!=-1){                String newData=new String(byteArray,0,readLength);                System.out.print(newData);                readLength=input.read(byteArray);            }            System.out.println();            input.close();        } catch (Exception e) {            // TODO: handle exception        }    }}

 

package com.ming.thread.pipeinputoutput;import java.io.PipedOutputStream;public class WriteData {    public void writeMethod(PipedOutputStream out){        try {            System.out.println("write :");            for(int i=0;i<300;i++){                String outData="" +(i+1);                out.write(outData.getBytes());                System.out.print(outData);            }            System.out.println();            out.close();        } catch (Exception e) {            // TODO: handle exception        }    }}

 

package com.ming.thread.pipeinputoutput;import java.io.PipedOutputStream;public class ThreadWrite extends Thread {    WriteData write;        PipedOutputStream out;        public ThreadWrite(WriteData write,PipedOutputStream out){        this.write=write;        this.out=out;    }        public void run(){        write.writeMethod(out);    }}

 

package com.ming.thread.pipeinputoutput;import java.io.PipedInputStream;public class ThreadRead extends Thread {    ReadData read;        PipedInputStream input;        public ThreadRead(ReadData read,PipedInputStream input){        this.read=read;        this.input=input;    }        public void run(){        read.readMethod(input);    }}
package com.ming.thread.pipeinputoutput;import java.io.PipedInputStream;import java.io.PipedOutputStream;/** * 管道流的一个测试 * @author mingge * */public class Run {    public static void main(String[] args) {        try {            WriteData writeData=new WriteData();            ReadData readData=new ReadData();            PipedInputStream input=new PipedInputStream();            PipedOutputStream out=new PipedOutputStream();            out.connect(input);            ThreadRead threadRead=new ThreadRead(readData, input);            threadRead.start();            Thread.sleep(2000);            ThreadWrite threadWrite=new ThreadWrite(writeData, out);            threadWrite.start();        } catch (Exception e) {            e.printStackTrace();        }    }}

 

 

(2)PipedReader与PipedWriter

package com.ming.thread.pipereaderwriter;import java.io.PipedReader;public class ReadData {    public void readMethod(PipedReader read){        try {            System.out.println("read :");            char[] byteArray=new char[20];            int readLength=read.read(byteArray);            while(readLength!=-1){                String newData=new String(byteArray, 0, readLength);                System.out.print(newData);                readLength=read.read(byteArray);            }            System.out.println();            read.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

 

package com.ming.thread.pipereaderwriter;import java.io.PipedWriter;public class WriteData {    public void writeMethod(PipedWriter out){        try {            System.out.println("write :");            for(int i=0;i<300;i++){                String outData=""+(i+1);                out.write(outData);                System.out.print(outData);            }            System.out.println();            out.close();        } catch (Exception e) {            e.printStackTrace();        }    }}
package com.ming.thread.pipereaderwriter;import java.io.PipedReader;public class ThreadReader extends Thread {    ReadData read;        PipedReader input;        public ThreadReader(ReadData read,PipedReader input){        this.read=read;        this.input=input;    }        public void run(){        read.readMethod(input);    }}
package com.ming.thread.pipereaderwriter;import java.io.PipedWriter;public class ThreadWrite extends Thread {    WriteData write;        PipedWriter out;        public ThreadWrite(WriteData write,PipedWriter out){        this.write=write;        this.out=out;    }        public void run(){        write.writeMethod(out);     }}
package com.ming.thread.pipereaderwriter;import java.io.PipedReader;import java.io.PipedWriter;public class Run {    public static void main(String[] args) {        try {            WriteData writeData=new WriteData();            ReadData readData=new ReadData();                        PipedReader inputStream=new PipedReader();            PipedWriter outputStream=new PipedWriter();            outputStream.connect(inputStream);                        ThreadReader threadRead=new ThreadReader(readData, inputStream);            threadRead.start();                        Thread.sleep(2000);                        ThreadWrite threadWrite=new ThreadWrite(writeData, outputStream);            threadWrite.start();                    } catch (Exception e) {            e.printStackTrace();        }    }}

 

就是看代码就可以了...

 

java多线程通过管道流实现不同线程之间的通信