zoukankan      html  css  js  c++  java
  • PipedOutputStream&PipedInputStream---管道流

    管道流通常与多线程技术相结合

    import java.io.IOException;
    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;
    
    public class PipedStreamDemo {
    
        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;
            try {
                len = pis.read(buf);
                String str = new String(buf,0,len);
                System.out.println(str);
                pis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }
    
    //定义输出任务
    class Output implements Runnable{
        PipedOutputStream pos;
        
        
        public Output(PipedOutputStream pos) {
            super();
            this.pos = pos;
        }
    
    
        @Override
        public void run() {
            
            //通过写方法完成
            try {
                pos.write("嘿,管道来了".getBytes());
                
                pos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
        
    }
  • 相关阅读:
    4.23计算机网络
    CF436F Banners
    CF1467C Three Bags
    LG P3247 [HNOI2016]最小公倍数
    LG P5473 [NOI2019] I 君的探险
    LG P3261 [JLOI2015]城池攻占
    LG P4149 [IOI2011]Race
    LG P3181 [HAOI2016]找相同字符
    SP7258 SUBLEX
    SP1811 LCS
  • 原文地址:https://www.cnblogs.com/qjlbky/p/5925751.html
Copyright © 2011-2022 走看看