我的 Server 类的主要方法中有以下关闭挂钩:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
if (open) {
open = false;
//log out all players
System.out.println("Logging out all players...");
Iterator playerIterator = playerList.values().iterator();
while (playerIterator.hasNext()) {
Player p = (Player) playerIterator.next();
playerIterator.remove();
p.logout("The server has been shut down.");
}
//save the World
System.out.println("Saving world...");
try {
String worldFile = Server.path.concat("/worlds/"+worldName);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(worldFile));
out.writeObject(world);
} catch (FileNotFoundException fe) {
System.out.println("World \"" + worldName + "\" couldn't be saved properly - world file not found.");
fe.printStackTrace();
} catch (IOException ioe) {
System.out.println("I/O error while attempting to save world \"" + worldName + "\".");
ioe.printStackTrace();
}
//close Server socket
try {
serverSocket.close();
System.out.println(worldName + " is now closed.");
} catch (IOException ioe) {
System.out.println("Failed to close ServerSocket.");
ioe.printStackTrace();
}
}
}
});
当我停止服务器(通过按 Ctrl+c)时,关闭挂钩会完成它应该做的所有事情(它工作得很好),但是我收到以下消息并且服务器不会完成关闭(我必须按 Ctrl+ c):
系统在应用程序的消息文件中找不到消息号 0x237b 的消息文本。
在我更新到 Java 7 之前,我不认为它曾经这样做过!任何想法为什么它可能会这样做?