可能是
ObjectOutputStream造成麻烦的原因。
如果
ObjectOutputStream在服务器上使用单个对象,则需要确保对其进行调用
reset,否则它将对先前编写的对象写入共享引用。这听起来像您所看到的。
为了说明问题:
class BrokenServer { void sendBrokenVoteData(ObjectOutputStream out) { out.writeObject(votes); changeVoteData(votes); out.writeObject(votes); // Writes a shared reference to "votes" WITHOUT updating any data. }}class FixedServer { void sendFixedVoteData(ObjectOutputStream out) { out.writeObject(votes); changeVoteData(votes); out.reset(); // Clears all shared references. out.writeObject(votes); // Writes a new copy of "votes" with the new data. }}


