這是my question here的一種擴展。如何修復ObjectInputStream和ObjectOutputStream的「寫入結束死亡」和「讀取結束死亡」錯誤?
我有3個班。
我的主:
import java.io.*;
public class ConnectionManager {
public static void main(String argv[]) {
try {
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pin = new PipedInputStream(pout);
Sender s = new Sender(pout,true);
Receiver r = new Receiver(pin,true);
System.out.println("Starting threads");
s.start();
r.start();
} catch (Exception e) {System.out.println(e);}
}
}
我的發件人/製作類:
import java.io.*;
public class Sender extends Thread {
ObjectOutputStream oos;
boolean primitive;
public Sender(OutputStream os, boolean primitive) {
try {
oos = new ObjectOutputStream(os);
this.primitive = primitive;
} catch (Exception e) {System.out.println(e);}
}
public void run() {
try {
System.out.println("Sending a message");
Thread.sleep(1000);
oos.writeInt(99);
oos.flush();
System.out.println("Message sent, terminating");
oos.close();
} catch (Exception e) {System.out.println("Sender: " + e);}
}
}
我的接收器/ Consumer類:
import java.io.*;
public class Receiver extends Thread {
ObjectInputStream ois;
boolean primitive;
public Receiver(InputStream is, boolean primitive) {
try {
ois = new ObjectInputStream(is);
this.primitive = primitive;
} catch (Exception e) {System.out.println(e);}
}
public void run() {
try {
System.out.println("waiting for a message");
int x = ois.readInt();
System.out.println("message received: " + x);
ois.close();
} catch (Exception e) {System.out.println("Receiver: " + e);}
}
}
產生這樣的輸出:
Starting threads
Sending a message
waiting for a message
Receiver: java.io.IOException: Write end dead
Sender: java.io.IOException: Read end dead
我在this page中讀到我收到這些異常,因爲我沒有關閉管道。但即使我這樣做,我仍然得到它們。我怎樣才能解決這個問題?
編輯:我將類型的流對象從PipedInputStream轉換爲InputStream,然後使用InputStream構造一個新的ObjectInputStream是因爲我希望能夠發送和接收各種類型的數據,而不是隻是int或字節。
他不需要使用'connect()'。他正在創建一個[連接的PipedInputStream'](https://docs.oracle.com/javase/8/docs/api/java/io/PipedInputStream.html#PipedInputStream-java.io.PipedOutputStream-)。 – EJP
你錯了。它*做*工作,並且我證明了它。你只需要*做對。* – EJP
核心點是在'run()'方法中創建對象流,這是你實際完成的。在重寫的start()方法中創建它們不會比OP的原始代碼更好。 – EJP