ObjectInputStream和ObjectOutputStream流在客户端、服务器程序之中是需要注意实例化顺序的,否则阻塞。不能两端同时先实例化ObjectInputStream流
案例
public class Server {
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
public void startServer() throws IOException, ClassNotFoundException {
ServerSocket serverSocket = new ServerSocket(54123);
System.out.println("服务器接收到侦听对象前");
Socket accept = serverSocket.accept();
System.out.println("服务器接收到侦听对象");
ois = new ObjectInputStream(accept.getInputStream());
oos = new ObjectOutputStream(accept.getOutputStream());
Student student = (Student) ois.readObject();
oos.writeObject(student);
}
}
public class Client {
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
public void connect() throws UnknownHostException, IOException, ClassNotFoundException {
Student student = new Student(1,"狗娃");
Socket socket = new Socket("127.0.0.1", 54123);
ois = new ObjectInputStream(socket.getInputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(student);
Student student2 = (Student) ois.readObject();
System.out.println(student2);
}
}
先开启服务器程序,在启动客户端程序,我们会发现两个程序都阻塞了,产生了死锁。然而在只需要更改任意一端的顺序就可以正常运行。
是什么原因导致这种结果呢?
通过查看api我们发现了ObjectInputStream的建立需要对端ObjectOutputStream建立时发送过来的Header数据。
如果两端都优先实例化ObjectInputStream流,两端都在等待对端发送的header,自然就阻塞了。
这是官方api的说明:
Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.
谷歌翻译:
创建一个从指定的InputStream读取的ObjectInputStream。 从流中读取序列化流头并进行验证。 此构造函数将阻塞,直到相应的ObjectOutputStream已写入并刷新标头。
最后这种特性在File流和Data流以及NIO中都是没有体现的。