使用PipedInputStream java编写end dead exception

在以下情况下发生写入结束exception:两个线程:

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写管道,这也是不稳定的。 如何避免这个问题并解决它? 谢谢

如果您有以下情况,将会出现“写死亡”例外情况:

  • PipedInputStream连接到PipedOutputStream和
  • 这些管道的末端由两个不同的线程读/写
  • 螺纹完成而不关闭管道的侧面。

要解决此exception,只需在完成向管道流写入和读取字节后,在Thread的runnable中关闭Piped Stream。

以下是一些示例代码:

 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/

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