源地址: http://blog.csdn.net/redhat456/article/details/4353505

用Socket通信的时候如果要用到ObjectInputStream 与 ObjectOutputStream的话,要确保Server端与Client端ObjectInputStream 与 ObjectOutputStream生成的顺序。

      在Server(Client)端生成一个ObjectInputStream流时必须确保在Client(Server)端生成一个ObjectOutputStream流。

      根据上述原则,合法的可能生成顺序有

      1、

            Server                          Client

            ObjectInputStream      ObjectOutputStream

            ObjectOutputStream    ObjectInputStream

 

      2、

            Server                          Client

            ObjectOutputStream    ObjectInputStream

            ObjectInputStream      ObjectOutputStream

 

     其他情况都会引起阻塞。

 

    引用:

           public ObjectInputStream(InputStream in)
 throws IOException

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.

If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission when invoked directly or indirectly by the constructor of a subclass which overrides the ObjectInputStream.readFields or ObjectInputStream.readUnshared methods.