在网络通讯中,主机与客户端若使用ObjectInputStream与ObjectOutputStream建立对象通讯,必须注重声明此两个对象的顺序。
如:
主机端先建立ObjectInputStream后建立ObjectOutputStream,则对应地客户端要先建立ObjectOutputStream后建立ObjectInputStream,否则会造成两方互相等待数据而导致死锁。
原因是建立ObjectInputStream对象是需要先接收一定的header数据,接收到这些数据之前会处于阻塞状态。以下为java 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.
如:
主机端先建立ObjectInputStream后建立ObjectOutputStream,则对应地客户端要先建立ObjectOutputStream后建立ObjectInputStream,否则会造成两方互相等待数据而导致死锁。
原因是建立ObjectInputStream对象是需要先接收一定的header数据,接收到这些数据之前会处于阻塞状态。以下为java 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.
对象通讯中主机与客户端的正确顺序
在使用ObjectInputStream与ObjectOutputStream进行对象通讯时,主机与客户端必须遵循正确的顺序来建立连接,否则可能导致死锁。创建ObjectInputStream前需要接收header数据,此过程会阻塞直至对应的ObjectOutputStream写入并刷新header。
703

被折叠的 条评论
为什么被折叠?



