0

我的客户端有以下方法:

  public boolean save() {
    this.ostream.writeObject(Command.SAVE); // write to socket
    return this.istream.readBoolean();
  }

在我的服务器中:

    Object o = this.istream.readObject();
    if (o == Command.SAVE) {
      boolean isSaved = save(); // save to database
      this.ostream.writeBoolean(isSaved);
    }

如果我使用readBooleanand writeBooleanreadBoolean客户端中的方法会阻塞,但如果我使用readObjectand writeObject,我的应用程序可以正常工作。readBoolean该方法是否有阻塞的原因?

4

1 回答 1

0

它更像是评论,但代码示例很难阅读,所以我将其发布为答案......希望它不会打扰任何人

你确定?我刚刚通过此代码对其进行了测试

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("d:/objects.obj"));
System.out.println("writeObject(true)");
out.writeObject(true);
System.out.println("writeBoolean(true)");
out.writeBoolean(true);
System.out.println("writeBoolean(true)");
out.writeBoolean(true);
out.close();//or flush
System.out.println("------------------");

ObjectInputStream in=new ObjectInputStream(new FileInputStream("d:/objects.obj"));
System.out.println("readObject="+in.readObject());
System.out.println("readBoolean="+in.readBoolean());
System.out.println("readObject="+in.readObject());//<- readObject stored by writeBoolean

它接缝工作正常。我遇到的唯一问题是当我尝试readObject存储时,writeBoolean反之亦然。readBooleanwriteObject

也许您只需要刷新输出流。

于 2012-07-28T23:18:29.877 回答