2
在什么情况下读取结束可以在对夫妇PipedOutputStream
和PipedInputStream
死?我没有关闭任何管道。IOException:读取结束死亡
在什么情况下读取结束可以在对夫妇PipedOutputStream
和PipedInputStream
死?我没有关闭任何管道。IOException:读取结束死亡
我在代码中遇到了java.io.IOException: Read end dead
,发现原因。在下面发布示例代码。如果您运行代码,您将得到“读取结束死亡”异常。如果仔细观察,消费者线程会从流中读取“hello”并终止;同时制片人睡了2秒钟,试图写出“世界”但失败。一个相关的问题在这里解释:http://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/
class ReadEnd {
public static void main(String[] args) {
final PipedInputStream in = new PipedInputStream();
new Thread(new Runnable() { //consumer
@Override
public void run() {
try {
byte[] tmp = new byte[1024];
while (in.available() > 0) { // only once...
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}).start();
PipedOutputStream out = null;
try {
out = new PipedOutputStream(in);
out.write("hello".getBytes());
Thread.sleep(2 * 1000);
out.write(" world".getBytes()); //Exception thrown here
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
}