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

    package piped.cn;
    
    import java.io.IOException;
    
    /*
     * 管道流:可以进行两个线程之间的通信
     * 管道输出流:PipedOutputStream
     * 管道输入流  类 PipedInputStream
     * 要实现管道输出,则必须将输出流连接在输入流上
     * 
     */
    /*   管道输出流:PipedOutputStream
     * 构造方法:
     * PipedOutputStream() 
              创建尚未连接到管道输入流的管道输出流。
             主要 方法:
              void    close() 
              关闭此管道输出流并释放与此流有关的所有系统资源。
     void    connect(PipedInputStream snk) 
              将此管道输出流连接到接收者。
     void    flush() 
              刷新此输出流并强制写出所有缓冲的输出字节。
     void    write(byte[] b, int off, int len) 
              将 len 字节从初始偏移量为 off 的指定 byte 数组写入该管道输出流。
     void    write(int b) 
              将指定 byte 写入传送的输出流。
              
     * 
     */
    /*   管道输入流  类 PipedInputStream
     * 构造方法:
     * PipedInputStream() 
              创建尚未连接的 PipedInputStream。
              主要的方法:
               int    available() 
              返回可以不受阻塞地从此输入流中读取的字节数。
     void    close() 
              关闭此管道输入流并释放与该流相关的所有系统资源。
     void    connect(PipedOutputStream src) 
              使此管道输入流连接到管道输出流 src。
     int    read() 
              读取此管道输入流中的下一个数据字节。
     int    read(byte[] b, int off, int len) 
              将最多 len 个数据字节从此管道输入流读入 byte 数组。
    protected  void    receive(int b) 
              接收数据字节。
     * 
     */
    
    
    public class PipedDemo {
        public static void main(String[] args) {
            //创建 send receive 对象
            Send s = new Send();
            Receive  r = new Receive ();
            //使用 PipedOutputStream 中的 void    connect(PipedInputStream snk) 方法连接
            try {
                s.getPost().connect(r.gitPis());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //开启线程
            new Thread(s).start();
            new Thread(r).start();
        }
    
    }
    package piped.cn;
    
    import java.io.IOException;
    import java.io.PipedInputStream;
    
    class Receive implements Runnable {
        //通过构造函数实例化 管道流输入对象
        PipedInputStream pis = null;
        //通过构造方法 实例化对象
        public Receive(){
          pis = new PipedInputStream();
        }
        public void run(){
            //开辟一个存储空间
            byte[] by = new byte[1024];
            int len = 0 ;
          try {
            len =  pis.read(by);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            try {
                pis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("输出的内容为:"+new String(by,0,len));
            
        }
        //创建获得线程接收类
        public PipedInputStream gitPis(){
            return this.pis;
        }
    
    }
    package piped.cn;
    
    import java.io.IOException;
    import java.io.PipedOutputStream;
    
    //创建一个发送信息的线程
    class Send implements Runnable{
        //输出管道流对象
        PipedOutputStream pos = null;
        //通过构造函数实例化 管道流输出对象
        public Send(){
            this.pos = new PipedOutputStream();
        }
      public void run(){
          //定义一个字符串
          String str = "hello wangyuanfang,hahahha";
          try {
              //write()接收的参数类型是byte,所以要转一下
            this.pos.write(str.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          //关闭流
          try {
            this.pos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          //通过线程类得到输出流
         
      }
      public PipedOutputStream getPost(){
            return this.pos; 
      }
    }
  • 相关阅读:
    配置hive的元数据到Mysql中
    对部分spi控制的外设芯片需要断电再上电处理
    驱动芯片L9945的odd parity的计算方法
    bit field不具备可移植性
    multiple storage classes in declaration specifiers报错解决
    C编译器中对h文件的重重包含导致的预处理不同的问题
    linker file链接文件
    eclipse编辑器中的TODO和FIXME的使用
    解决hightec的链接文件的路径问题
    LIN开发
  • 原文地址:https://www.cnblogs.com/yuanyuan2017/p/6944786.html
Copyright © 2011-2022 走看看