gpt4 book ai didi

java - IOException:读取结束死

转载 作者:行者123 更新时间:2023-12-04 05:51:07 27 4
gpt4 key购买 nike

什么情况下read end可以死对偶PipedOutputStreamPipedInputStream ?我没有关闭任何管道。

最佳答案

我遇到了java.io.IOException: Read end dead在我的代码中并找出原因。在下面发布示例代码。如果您运行代码,您将收到“读取结束死机”异常。如果仔细观察,消费者线程从流中读取“hello”并终止;同时生产者 hibernate 2 秒并尝试写入“world”但失败。这里解释了一个相关的问题: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 {
}
}

}

关于java - IOException:读取结束死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10035168/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com