• 管道流 PipedInputStream和PipedOutputStream 用法


    注意管道流需要用到多线程

    package test;
     
     
    import java.io.*;
    import java.sql.SQLClientInfoException;
    import java.text.DateFormat;
    import java.text.ParsePosition;
    import java.text.SimpleDateFormat;
    import java.util.*;
    import java.util.concurrent.CountDownLatch;
    import java.util.zip.InflaterInputStream;
     
    import javax.annotation.processing.FilerException;
    import javax.management.RuntimeErrorException;
    
    import privateclass.Filterby_Name;
    import privateclass.Filterby_hidden;
    import privateclass.Filterby_java;
    import privateclass.MyBufferedReader;
    import privateclass.Person;
     
    public class Main {
     
        private static final String space_operator = " ";
        private static final double pi = Math.PI;
        private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    	private static final int SIZE = 1024*1024;
    
    	/*
    	 * 管道流需要配合多线程
    	 * 因为单线程容易出现死锁
    	 * 因为当在一个在进行另外一个就会阻塞
    	 * 我们再用管道的时候一定要记得连接
    	 * connect
    	 */
        public static void main(String[] args) throws Exception {
    
        	PipedInputStream pis = new PipedInputStream();
        	PipedOutputStream pos = new PipedOutputStream();
        	pis.connect(pos);
        	Thread t1 = new Thread(new Input(pis));
        	Thread t2 = new Thread(new Output(pos));
        	t1.start();
        	t2.start();
        	
        	
        }
    
    	
    }
    
    class Input implements Runnable
    {
    
    	private PipedInputStream pis;
    	
    	public Input(PipedInputStream pis) {
    		super();
    		this.pis = pis;
    	}
    	@Override
    	public void run() {
    		
    		byte buf[] = new byte[1024];
    		try {
    			pis.read(buf);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		String s = new String(buf);
    		System.out.println(s);
    	}
    	
    }
    
    class Output implements Runnable
    {
    	private PipedOutputStream pos;
    	
    	public Output(PipedOutputStream pos) {
    		super();
    		this.pos = pos;
    	}
    
    	@Override
    	public void run() {
    		/*
    		 * 让线程在这里睡一会
    		 * 你会发现更好的观察管道之间的联系
    		 */
    		try {
    			Thread.sleep(5000);
    		} catch (InterruptedException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		try {
    			pos.write("我是写入管道2333".getBytes());
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    }
    

      

  • 相关阅读:
    @MapperScan 注解和 mybatis.mapper-locations 配置两者缺一不可
    com.mysql.cj.jdbc.Driver和com.mysql.jdbc.Driver的区别
    定时任务框架Quartz-(一)Quartz入门与Demo搭建
    js分页的实现代码
    圣杯布局中对left盒子设置负内边距-100%的一点解释
    前端小知识--为什么你写的height:100%不起作用?
    CSS:实现垂直居中的常用方法
    [jdk源码阅读系列]Java中System.arraycopy()的用法
    [jdk源码阅读系列]overflow-conscious code
    省选?
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11828563.html
Copyright © 2020-2023  润新知