使用PipedInputStream java编写end dead exception

时间:2011-03-28 00:28:03

标签: java multithreading ioexception

在以下情况下发生写死终止异常: 两个线程:

A: PipedOutputStream put = new PipedOutputStream();
   String msg = "MESSAGE";
   output.wirte(msg.getBytes());
   output.flush();

B: PipedInputStream get = new PipedOutputStream(A.put);  
   byte[] get_msg = new byte[1024];
   get.read(get_msg);

情况如下:A和B同时运行,A写入管道,B读取它。 B只是从管道中读取并清除了该管道的缓冲区。然后A不会在未知的时间间隔内将msg写入管道。但是,在某一时刻,B再次读取管道并发生java.io.IOException: write end dead,因为管道的缓冲区仍为空。而且我不想睡眠()线程B等待A写管道,这也是不稳定的。如何避免这个问题并解决它?谢谢

2 个答案:

答案 0 :(得分:17)

当你有:

时,会出现“写死亡”异常
  • 连接到PipedOutputStream和
  • 的PipedInputStream
  • 这些管道的末端由两个不同的线程读/写
  • 螺纹在没有关闭管道侧面的情况下完成。

要解决此异常,只需在完成向管道流中写入和读取字节后,在线程的runnable中关闭管道流。

以下是一些示例代码:

final PipedOutputStream output = new PipedOutputStream();
final PipedInputStream input = new PipedInputStream(output);

    Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                output.write("Hello Piped Streams!! Used for Inter Thread Communication".getBytes());                       
                output.close();

            } catch(IOException io) {
                io.printStackTrace();
            }                   
        }
    });

    Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {

            try {

                int data;

                while((data = input.read()) != -1) {
                    System.out.println(data + " ===> " + (char)data);
                }

                input.close();

            } catch(IOException io) {
                io.printStackTrace();
            } 

        }
    });

    thread1.start();
    thread2.start();

完整代码在此处:https://github.com/prabhash1785/Java/blob/master/JavaCodeSnippets/src/com/prabhash/java/io/PipedStreams.java

有关详情,请查看这个不错的博客:https://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/

答案 1 :(得分:4)

你需要在编写线程完成之前关闭PipedOutputStream(并且在写完所有数据之后)。当没有写入线程并且写入器没有正确关闭时,PipedInputStream会在read()上抛出此异常