博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java多线程通过管道流实现不同线程之间的通信
阅读量:6830 次
发布时间:2019-06-26

本文共 5462 字,大约阅读时间需要 18 分钟。

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

 

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

 

转载地址:http://sjnkl.baihongyu.com/

你可能感兴趣的文章
php安装
查看>>
linux之ls命令
查看>>
json框架
查看>>
apache启动报错(98)Address already in use: make_sock: could not bind to address [::]:80
查看>>
我的友情链接
查看>>
shorewall 企业防火墙的完美实现
查看>>
nginx反向代理负载均衡
查看>>
CentOS 7中没有ifconfig命令,而且不能发现eth0
查看>>
GCC内嵌汇编语法
查看>>
从1,2,3,4,5,6,7,8,9选出m位求巧妙平方数
查看>>
我说,菜鸟(创业篇)
查看>>
CentOS下安装beanstalkd服务
查看>>
xcode程序运行时出现Please check your setup and again
查看>>
无敌服务器交互脚本
查看>>
ubuntu 安装 downloading 问题
查看>>
apache 404\403错误页面跳转
查看>>
virtualbox 4..1以后 虚拟磁盘的uuid怎么修改
查看>>
在单链表中删除倒数第k个节点
查看>>
按升序对栈进行排序
查看>>
OLE技术及组件对象模型(COM)
查看>>