我正在尝试让用户 ' synchronized(object) {object.wait()}
' 暂停我的服务器,直到对象准备好。我synchronized(object) {object.notify() }
在类中调用了“”,它将设置对象值。但似乎我的服务器在wait()
.
public class MyServer {
...
do {
try {
message = (String) in.readObject();
System.out.println("server receive>" + message);
synchronized (myServerSend) {
myServerSend.wait();
}
System.out.println("Am I wake up???"); //this never print out
sendMessage(myServerSend);
} catch (ClassNotFoundException classnot) {
System.err.println("Data received in unknown format");
}
} while (!message.equals("bye"));
} catch (IOException ioException) {
ioException.printStackTrace();
}
....
}
这是我设置对象值的要求
public class Action implements MouseInputListener, MouseMotionListener {
....
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {...
}
else {
....
if(MyGame.currentPlayer == MyGame.WHITE) {
myServerSend = "" + chosenPieceIndex + "," + moveLocationIndex;
synchronized (myServerSend) {
myServerSend.notify(); // this seems like fail to wake up myServer
}
System.out.println(myServerSend);
}
如果我将 myServerSend 声明为公共静态字符串类型会不会有问题?有人可以帮忙吗?提前致谢!