我有以下 RMI 服务器代码:
public class ServerProgram {
public ServerProgram() {
try {
LocateRegistry.createRegistry(1097);
Calculator c = new CalculatorImpl();
String name = "rmi://host:port/name";
Naming.rebind(name, c);
System.out.println("Service is bound......");
} catch (Exception e) {
}
}
public static void main(String[] args) {
new ServerProgram();
}
}
当上述程序运行时,它一直运行以等待客户端请求。但我不明白的是,是什么让该程序在不处于类似 while(true){};
的状态时继续运行,以及如何阻止它监听,除了停止整个程序?
最佳答案
使它保持运行的是一个由 RMI 启动的非守护程序监听线程。要使其退出,使用 UnicastRemoteObject.unexportObject() 取消绑定(bind)名称并取消导出注册表和远程对象。
关于java - 是什么让 rmi 服务器保持运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7446035/