问题描述
这是.
我有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);}
}
}
我的发件人/制作人班:
My Sender/Producer class:
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);}
}
}
我的接收器/消费者类:
My Receiver/Consumer class:
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
我在此页面中读到我正在获取这些例外,因为我没有关闭管道.但是即使我这样做,我仍然会得到它们.我该如何解决?
I read in this page that I'm getting these exceptions because I wasn't closing the pipes. But even if I do, I still get them. How can I fix this?
edit:之所以将流对象的类型从例如PipedInputStream转换为InputStream,然后使用InputStream构造新的ObjectInputStream是因为我希望能够发送和接收各种类型的数据,而不仅仅是int或字节.
edit: the reason I'm casting the type of the stream objects from, say, PipedInputStream to InputStream and then constructing a new ObjectInputStream using InputStream is because I want to be able to send and receive data of various types, not just int or bytes.
推荐答案
我保持纠正;并遵循EJP的建议;这是一个可行的解决方案.
I stay corrected; and following the advise by EJP; here is a working solution.
import java.io.*;
public class ConnectionManager {
public static void main(String argv[]) throws Exception {
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pin = new PipedInputStream(pout);
Sender s = new Sender(pout);
Receiver r = new Receiver(pin);
System.out.println("Starting threads");
s.start();
r.start();
}
}
class Sender extends Thread {
private final OutputStream os;
Sender(OutputStream os) { this.os = os; }
public void run() {
try(ObjectOutputStream oos = new ObjectOutputStream(os)) {
oos.writeInt(99);
System.out.println("Message sent, terminating");
} catch (Exception e) {
System.out.println("Sender: " + e);
e.printStackTrace();
}
}
}
class Receiver extends Thread {
private final InputStream is;
Receiver(InputStream is) {this.is = is; }
public void run() {
try(ObjectInputStream ois = new ObjectInputStream(is)) {
System.out.println("waiting for a message");
int x = ois.readInt();
System.out.println("message received: " + x);
} catch (Exception e) {
System.out.println("Receiver: " + e);
e.printStackTrace();
}
}
}
应打印:
Starting threads
Message sent, terminating
waiting for a message
message received: 99
注意:核心点是在 run 方法内创建 ObjectInputStreams .除此之外:删除了不必要的东西(该布尔原语;但添加了try-with-resources和打印堆栈跟踪).
Notes: the core point is to create the ObjectInputStreams within the run methods. Beyond that: removed the things not necessary (that boolean primitive; but added try-with-resources and printing stack traces).
这篇关于我该如何解决“写入结束"问题?和“读取结束时死".错误的ObjectInputStream和ObjectOutputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!