java io 笔记三:PipedOutputStream、PipedInputStream类

使用管道流类、可以实现各个程序模块之间的松耦合通信

PipedOutputStream发送者线程:

  
  
  
  
  1. package com.boxun.hzw.file;   
  2.    
  3. import java.io.IOException;   
  4.    
  5. import java.io.PipedOutputStream;   
  6. @SuppressWarnings("all")   
  7. /**  
  8.  * 发送者线程  
  9.  *   
  10.  * 线程 是程序中的执行线程。Java 虚拟机允许应用程序并发地运行多个执行线程。   
  11.  *   
  12.  */   
  13. public class Sender extends Thread {   
  14.        
  15.     /*  
  16.      * PipedOutputStream类:  
  17.      * 可以将管道输出流连接到管道输入流来创建通信管道。管道输出流是管道的发送端。  
  18.      * 通常,数据由某个线程写入 PipedOutputStream 对象,  
  19.      * 并由其他线程从连接的 PipedInputStream 读取。  
  20.      * 不建议对这两个对象尝试使用单个线程,因为这样可能会造成该线程死锁。  
  21.      * 如果某个线程正从连接的管道输入流中读取数据字节,  
  22.      * 但该线程不再处于活动状态,则该管道被视为处于 毁坏 状态。  
  23.      */   
  24.     private PipedOutputStream out = new PipedOutputStream();   
  25.        
  26.     public PipedOutputStream getOutputStream(){   
  27.         return out;   
  28.     }   
  29.        
  30.     /**  
  31.      * 因为这里继承了Thread类!所以必须从写run方法  
  32.      * 如果该线程是使用独立的 Runnable 运行对象构造的,  
  33.      * 则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。  
  34.      *   
  35.      */   
  36.     public void run(){   
  37.         String strInfo = "hello my Hzw" ;   
  38.         try {   
  39.                
  40.             out.write(strInfo.getBytes());   
  41.             out.close();   
  42.         } catch (IOException e) {   
  43.             e.printStackTrace();   
  44.         }   
  45.     }   
PipedInputStream接收者线程:

  
  
  
  
  1. package com.boxun.hzw.file;   
  2.    
  3. import java.io.IOException;   
  4.    
  5. import java.io.PipedInputStream;   
  6.    
  7. @SuppressWarnings("all")   
  8. /**  
  9.  * 接收者线程  
  10.  */   
  11. public class Receiver extends Thread {   
  12.        
  13.     /*  
  14.      * PipedInputStream类:  
  15.      * 管道输入流应该连接到管道输出流;管道输入流提供要写入管道输出流的所有数据字节。  
  16.      * 通常,数据由某个线程从 PipedInputStream 对象读取,  
  17.      * 并由其他线程将其写入到相应的 PipedOutputStream。  
  18.      * 不建议对这两个对象尝试使用单个线程,因为这样可能死锁线程。  
  19.      * 管道输入流包含一个缓冲区,可在缓冲区限定的范围内将读操作和写操作分离开。  
  20.      * 如果向连接管道输出流提供数据字节的线程不再存在,则认为该管道已损坏。  
  21.      */   
  22.     private PipedInputStream in = new PipedInputStream();   
  23.    
  24.     /*  
  25.      * 获得实例方法  
  26.      */   
  27.     public PipedInputStream getInputStream(){   
  28.         return in;   
  29.     }   
  30.        
  31.     public void run(){   
  32.         byte[] buf = new byte[1024];   
  33.         try {   
  34.             int len = in.read(buf);   
  35.             System.out.println(new String(buf,0,len));   
  36.             in.close();   
  37.         } catch (IOException e) {   
  38.             e.printStackTrace();   
  39.         }   
  40.     }   

main方法:

  
  
  
  
  1. package com.boxun.hzw.file;   
  2.    
  3. import java.io.*;   
  4. @SuppressWarnings("all")   
  5. /**  
  6.  * 使用管道流类、可以实现各个程序模块之间的松耦合通信  
  7.  */   
  8. public class PipedStreamTest {   
  9.    
  10.     /**  
  11.      * @param args  
  12.      */   
  13.     public static void main(String[] args) {   
  14.         Sender t1 = new Sender();   
  15.            
  16.         Receiver t2 = new Receiver();   
  17.            
  18.         PipedOutputStream out = t1.getOutputStream();   
  19.            
  20.         PipedInputStream in = t2.getInputStream();   
  21.            
  22.         try {   
  23.             //管道链接   
  24.             out.connect(in);   
  25.                
  26.             /**  
  27.              * Thread类的START方法:  
  28.              * 使该线程开始执行;Java 虚拟机调用该线程的 run 方法。   
  29.              * 结果是两个线程并发地运行;当前线程(从调用返回给 start 方法)和另一个线程(执行其 run 方法)。   
  30.              * 多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。   
  31.              */   
  32.             t1.start();   
  33.             t2.start();   
  34.         } catch (IOException e) {   
  35.             e.printStackTrace();   
  36.         }   
  37.     }   
  38.    
  39. }  

你可能感兴趣的:(java,IO,职场,休闲)