zoukankan      html  css  js  c++  java
  • 021.14 IO流 管道流

    用的频率不高
    特点:读取管道和写入管道对接,需要是用多线程技术,单线程容易死锁

    使用connect方法连接两个流,实现边读编写,和node.js的管道流差不多

    //##主函数位置
    public static void main(String[] args) throws IOException
    {
        //创建两个管道对象
        PipedInputStream pis = new PipedInputStream();
        PipedOutputStream pos = new PipedOutputStream();
        
        //将两个管道连接上
        pis.connect(pos);
        
        new Thread(new Input(pis)).start();;
        new Thread(new Output(pos)).start();
    }
    
    
    //定义输入任务
    class Input implements Runnable{
        private PipedInputStream pis;
        
        public Input(PipedInputStream pis) {
            super();
            this.pis = pis;
        }
    
        @Override
        public void run()
        {
            byte[] buf = new byte[1024];
            int len = 0;
            try {
                len = pis.read(buf);
                String messege = new String(buf,0,len);
                System.out.println(messege);
                pis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    //定义输出任务
    class Output implements Runnable{
        private PipedOutputStream pos;
        
        public Output(PipedOutputStream pos) {
            super();
            this.pos = pos;
        }
    
        @Override
        public void run()
        {
            try {
                pos.write("hei,我很想你".getBytes());
                pos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    MR案例:内连接代码实现
    分布式缓存DistributedCache
    MR案例:Map-Join
    hadoop随手笔记
    Job流程:决定map个数的因素
    Job流程:提交MR-Job过程
    MR案例:Reduce-Join
    MR案例:倒排索引
    MR案例:路径过滤PathFilter
    MR案例:分区和排序
  • 原文地址:https://www.cnblogs.com/-nbloser/p/9626481.html
Copyright © 2011-2022 走看看