Record final exception using PipedInputStream java

Recording a dead center exception occurs in the following situation: Two threads:

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

Here is the situation: A and B start at the same time, and A writes to the pipe, and B reads it. B, just read from the pipe, and the buffer of this pipe is cleared. Then A does not write msg to the pipe in an unknown interval. However, at some point, B reads the pipe again, and java.io.IOException: write end dead occurs because the pipe buffer is still empty. And I do not want to sleep () thread B to wait for A to write a pipe, which is also unstable. How to avoid this problem and solve it? thanks

+6
java multithreading ioexception
source share
4 answers

"Record the end of the dead" will throw exceptions if you have:

  • PipedInputStream connected to PipedOutputStream and
  • The ends of this channel are read / written by two different streams.
  • Threads end without closing their sides.

To eliminate this exception, simply close your stream stream in the Thread stream after you have finished writing and reading bytes to / from the stream.

Here is a sample code:

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

The full code is here: https://github.com/prabhash1785/Java/blob/master/JavaCodeSnippets/src/com/prabhash/java/io/PipedStreams.java

For more information, please take a look at this nice blog: https://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/

+12
source share

you need to close the PipedOutputStream before writing the stream is complete (and, of course, after all the data has been written). PipedInputStream throws this exception when reading () when there is no write thread and the record is not closed properly

+4
source share

I had the same problem in one of my projects. I created another implementation of the PipedInputStream and PipedOutputStream classes. You can find them on Github at https://github.com/LenraOfficial/lenra-java-io

0
source share

You may need to implement some kind of locking mechanism. You can start by looking at java.util.concurrent.Semaphore or java. util.concurrent.locks.LockSupport for basic things that can suit your needs.

-one
source share

All Articles