JAVA IO-管道流

来源:互联网 发布:软考初级程序员视频 编辑:程序博客网 时间:2024/05/16 18:53

JAVA IO-管道流

管道

  1. 现实中的管道可以理解为用管子、管子联接件和阀门等联接成的用于输送气体、液体或带固体颗粒的流体的装置百度百科-管道;JAVA中我们可以把Pipes理解成连接数据源和目标的媒介。
  2. JAVA IO中通过Pipes(管道)来在同一个JVM中的不同两个线程之间进行通信;JAVA IO中的Pipes不同于Unix/Linux下的Pipes,Unix/Linux下的Pipes是在不同地址下的两个进程之间可以通过Pipes进行通信;JAVA IO中的Pipes只能在同一个JVM下的两个不同线程之间进行通信。

管道流

JAVA IO中通过PipedOutputStream/PipedInputStream来创建管道;一个线程通过PipedOutputStream写入数据,另一个线程通过PipedInputStream来读取数据;PipedOutputStream/PipedInputStream可以通过connect()来进行关联。

死锁问题

两个关联的管道流,务必分配到不同的线程中去;因为PipedOutputStream的write()和PipedInputStream的read()方法会导致流的阻塞,一个线程中同时读写就会导致死锁问题。

JAVA IO管道流例子

public class PipedStream {    public static void main(String[] args) {        PipedReceiver pipedReceiver = new PipedReceiver();        PipedSender pipedSender = new PipedSender();        try {            pipedSender.getPipedOutputStream().connect(                    pipedReceiver.getPipedInputStream());        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        pipedSender.sendMsg("hello world!sky".getBytes());        pipedReceiver.start();    }    /**     * PipedInputStream read     * @author Administrator     *     */    static class PipedReceiver extends Thread {        private PipedInputStream pipedInputStream;        private byte[] buffer = new byte[1024];        public PipedReceiver() {            pipedInputStream = new PipedInputStream();        }        public PipedInputStream getPipedInputStream() {            return pipedInputStream;        }        @Override        public void run() {            if (pipedInputStream != null) {                try {                    int count = pipedInputStream.read(buffer, 0, buffer.length);                    if (count > 0) {                        System.out.println("Count:" + count + "\r\n"                                + "Content:" + new String(buffer, 0, count));                    }                    pipedInputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * PipedOutputStream write     * @author Administrator     *     */    static class PipedSender extends Thread {        private PipedOutputStream pipedOutputStream;        private byte[] msg;        public PipedSender() {            pipedOutputStream = new PipedOutputStream();        }        public PipedOutputStream getPipedOutputStream() {            return pipedOutputStream;        }        public void sendMsg(byte[] msg) {            if (msg == null)                return;            this.msg = msg;            start();        }        @Override        public void run() {            if (pipedOutputStream != null && msg != null) {                try {                    pipedOutputStream.write(msg, 0, msg.length);                    pipedOutputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}
0 0