实际上,仅仅注销并立即调用System.Exit并不能完全关闭。在通知客户机消息已完成之前,它基本上断开了连接。工作原理是启动一个关闭系统的小线程,比如:
public void quit() throws RemoteException {
System.out.println("quit");
Registry registry = LocateRegistry.getRegistry();
try {
registry.unbind(_SERVICENAME);
UnicastRemoteObject.unexportObject(this, false);
} catch (NotBoundException e) {
throw new RemoteException("Could not unregister service, quiting anyway", e);
}
new Thread() {
@Override
public void run() {
System.out.print("Shutting down...");
try {
sleep(2000);
} catch (InterruptedException e) {
// I don't care
}
System.out.println("done");
System.exit(0);
}
}.start();
}
线程需要能够在将来发生某些事情的同时仍然从退出方法返回。