zoukankan      html  css  js  c++  java
  • JavaIO流学习总结-PipedOutputStream和PipedInputStream基本操作练习

    package io;
    import java.io.IOException;
    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;
    /**
     * 修改日期:2020/03/31
     * 修改人:牟松
     * PipedOutputStream和PipedInputStream基本操作练习
     * 管道流实现了两个线程之间的通信,多线程之间传输数据需要通信时可以用到管道流
     * 参考连接:https://blog.csdn.net/qq_24434671/article/details/90206417 
    **/
    public class PipdTest {
    public static void main(String[] args) throws IOException {
          // 创建一个发送者对象
          Sender sender = new Sender();
          // 创建一个接收者对象
          Receiver receiver = new Receiver();
          // 获取输出管道流
          PipedOutputStream outputStream = sender.getOutputStream();
          // 获取输入管道流
          PipedInputStream inputStream = receiver.getInputStream();
         // 链接两个管道,这一步很重要,把输入流和输出流联通起来
          outputStream.connect(inputStream);
         // 启动发送者线程
          sender.start();
         // 启动接收者线程
          receiver.start();
       }
    }
    /**
     * 发送线程
     * @author yuxuan
     */
    class Sender extends Thread {
         //声明一个 管道输出流对象 作为发送方
         private PipedOutputStream outputStream = new PipedOutputStream();
         public PipedOutputStream getOutputStream() {
             return outputStream;
         }
     
       @Override
       public void run() {
           String msg = "Hello World";
           try {
               outputStream.write(msg.getBytes());
           } catch (IOException e) {
                 e.printStackTrace();
           } finally {
               try {
               // 关闭输出流
                   outputStream.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
          }
       }
    }
     
    /**
     * 接收线程
     * @author yuxuan
     */
    class Receiver extends Thread {
         // 声明一个 管道输入对象 作为接收方
         private PipedInputStream inputStream = new PipedInputStream();
         public PipedInputStream getInputStream() {
              return inputStream;
         }
     
         @Override
         public void run() {
             byte[] buf = new byte[1024];
             try {
                 // 通过read方法 读取长度
                 int len = inputStream.read(buf);
                 System.out.println(new String(buf, 0, len));
             } catch (IOException e) {
                 e.printStackTrace();
             } finally {
                try {
                 // 关闭输入流
                 inputStream.close();
               } catch (IOException e) {
                  e.printStackTrace();
               }
           }
        }
    }
  • 相关阅读:
    HTB-靶机-Charon
    第一篇Active Directory疑难解答概述(1)
    Outlook Web App 客户端超时设置
    【Troubleshooting Case】Exchange Server 组件状态应用排错?
    【Troubleshooting Case】Unable to delete Exchange database?
    Exchange Server 2007的即将生命周期,您的计划是?
    "the hypervisor is not running" 故障
    Exchange 2016 体系结构
    USB PE
    10 months then free? 10个月,然后自由
  • 原文地址:https://www.cnblogs.com/musong1998/p/12606813.html
Copyright © 2011-2022 走看看