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
java multithreading ioexception
laynece
source share