zoukankan      html  css  js  c++  java
  • java:管道流(线程间管道流)

    class Send implements Runnable{
    
    	PipedOutputStream pos = null;
    	
    	public Send()
    	{
    		this.pos = new PipedOutputStream();
    	}
    	
    	public PipedOutputStream getPipedOutputStream()
    	{
    		return this.pos;
    	}
    	
    	@Override
    	public void run() {
    		// TODO 自动生成的方法存根
    		String str = "hello world!!!";
    		try {
    			this.pos.write(str.getBytes());
    		} catch (IOException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    		try {
    			this.pos.close();
    		} catch (IOException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    	}
    	
    }
    
    class Receive implements Runnable{
    	
    	PipedInputStream pis = null;
    
    	public Receive(){
    		this.pis = new PipedInputStream();
    	}
    	
    	public PipedInputStream getPipedInputStream()
    	{
    		return this.pis;
    	}
    	
    	@Override
    	public void run() {
    		// TODO 自动生成的方法存根
    		byte b[] = new byte[1024];
    		int len = 0;
    		try {
    			len = this.pis.read(b);
    		} catch (IOException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    		try {
    			this.pis.close();
    		} catch (IOException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    		System.out.println(new String(b,0,len) );
    	}
    	
    }
    
    public class PipedOutputStreamDemo {
    
    	public static void main(String[] args) throws Exception {
    		// TODO 自动生成的方法存根
    		Send send =  new Send();
    		Receive rec = new Receive();
    		send.getPipedOutputStream().connect(rec.getPipedInputStream());
    		new Thread(send).start();
    		new Thread(rec).start();
    	}
    
    }
    

      

  • 相关阅读:
    第六章 虹销雨霁(中)
    第四章 曙光初现(下)
    第三章 曙光初现(上)
    第二章 福祸相伴(下)
    第二章 福祸相伴(上)
    小云(云层-陈霁)的发展史
    小白成长建议(9)-苞丁解牛
    NYoj 116士兵杀敌(二)区间求和,单点更新
    HDU 1754 区间查询,单点更新
    《灯亮or灯灭》 --有个有趣的数论问题
  • 原文地址:https://www.cnblogs.com/achengmu/p/7143299.html
Copyright © 2011-2022 走看看