どのような状況で read end がカップルPipedOutputStream
とで死んでいる可能性がありPipedInputStream
ますか? 私はパイプを閉じていません。
6562 次
1 に答える
3
私は自分のコードで遭遇java.io.IOException: Read end dead
し、原因を突き止めました。以下にサンプルコードを投稿します。コードを実行すると、「Readenddead」例外が発生します。よく見ると、コンシューマスレッドはストリームから「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 {
}
}
}
于 2012-06-18T19:33:00.223 に答える