码迷,mamicode.com
首页 > 系统相关 > 详细

验证管道流:进程间通信

时间:2016-01-14 16:04:50      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

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

public class FileIO{
     public static void main(String []args){
         Send send=new Send();
         Recive recive=new Recive();
         
         try {
            send.getOut().connect(recive.getIn());
        } catch (IOException e) {
        
            e.printStackTrace();
        }
         new Thread(send).start();
         new Thread(recive).start();
     }    
}

class Send implements Runnable{
    private PipedOutputStream out=null;
    public Send(){out =new PipedOutputStream();}//构造方法
    public PipedOutputStream getOut(){return this.out;}  
    public void run() {
        String str="hello,Mr Zhou";
        try {
            out.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    
}
class Recive implements Runnable{
    private PipedInputStream in=null;
    public Recive(){in=new PipedInputStream();}
    public PipedInputStream getIn(){return this.in;}
    public void run() {
        byte []by=new byte[1024];
        int len = 0;
        try {
             len=in.read(by);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("接受内容为:"+(new String(by,0,len)));
    }
    
}

 

验证管道流:进程间通信

标签:

原文地址:http://www.cnblogs.com/gugibv/p/5130259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!