Suz*_*ioc 4 java pipe ioexception
在什么情况下 read end 会死在情侣PipedOutputStreamand中PipedInputStream?我不会关闭任何管道。
我java.io.IOException: Read end dead在我的代码中遇到了并找出了原因。下面发布示例代码。如果运行代码,您将收到“Read end dead”异常。如果仔细观察,消费者线程会从流中读取“hello”并终止;与此同时,制作人休眠了 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 {
}
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
7736 次 |
| 最近记录: |