13

Write end dead 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 run concurrently, and A writes to the pipe and B reads it. B just read from the pipe and buffer of this pipe is cleared. Then A doesn't write msg to the pipe in unknown interval. However, at one moment, B read the pipe again and java.io.IOException: write end dead occurs, because the buffer of the pipe is still empty. And I don't want to sleep() thread B to wait for A writing the pipe, which is also unstable. How to avoid this problem and solve it? Thanks

laynece
  • 231
  • 1
  • 3
  • 6
  • 2
    http://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/ – Romain Hippeau Mar 28 '11 at 00:50
  • 1
    Hey, if you're still on SO please accept one of the answers. They solved my problem, they deserve to be upvoted + accepted, and it will increase their visibility for future readers – Dici Dec 03 '15 at 17:32

2 Answers2

25

"Write end dead" exceptions will arise when you have:

  • A PipedInputStream connected to a PipedOutputStream and
  • The ends of these pipe are read/written by two different threads
  • The threads finish without closing their side of the pipe.

To resolve this exception, simply close your Piped Stream in your Thread's runnable after you have completed writing and reading bytes to/from the pipe stream.

Here is some 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();

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

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

user207421
  • 305,947
  • 44
  • 307
  • 483
Prabhash Rathore
  • 709
  • 10
  • 18
6

you need to close PipedOutputStream, before writing thread is finished (and ofcourse after all data is written). PipedInputStream throws this exception on read() when there is no writing thread and writer is not properly closed

marek
  • 61
  • 1
  • 2