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();
    	}
    
    }
    

      

  • 相关阅读:
    python面试题总结与分析(一)
    Word2vec ------算法岗面试题
    深度学习(二)-----算法岗面试题
    深度学习(一)-------算法岗面试题
    模型融合和提升的算法------算法面试题
    前后端交互3 发送消息1
    前后端交互2 vuex存储token
    前后端交互1
    4.弹出层组件的实现与封装
    3. 聊天列表页面的实现
  • 原文地址:https://www.cnblogs.com/achengmu/p/7143299.html
Copyright © 2011-2022 走看看