ObjectInputStream 死锁问题

来源:互联网 发布:linux 部分中文乱码 编辑:程序博客网 时间:2024/04/30 12:57

  1. ObjectInputStream与ObjectOutputStream的顺序问题
在网络通讯中,主机与客户端若使用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.

 

 

http://tech.cncms.com/web/java/32345.html

原创粉丝点击