是什么让rmi服务器继续运行?
我有以下 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){};
之类的情况下继续运行,以及如何阻止它监听,除了停止整个程序?
I have the following RMI server code:
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();
}
}
When the above program running, it keeps running to wait for client requests. But what I do not understand is what make that program keeps running while it is not in something like while(true){};
and how to stop it from listening, except stopping the whole program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让它继续运行的是一个由 RMI 启动的非守护进程监听线程。要使其退出,请使用 UnicastRemoteObject.unexportObject() 取消名称绑定并取消注册表和远程对象的导出。
What makes it keep running is a non-daemon listening thread started by RMI. To make it exit, unbind the name and unexport both the Registry and the remote object, with UnicastRemoteObject.unexportObject().
要停止它,你应该打电话
To stop it, you should call
这是由编辑非-编辑
守护线程
完成的。请参阅:Java 中的守护线程是什么?您可以使用这个小示例来测试行为:
该设置阻止 JVM 关闭。在
System.out.println("Finished");
之后,您仍然会看到线程正在运行,并带有它的"Woke up"
日志输出。This is done by a edit non-edit
daemon thread
. See: What is Daemon thread in Java?You can test the behavior with this little example:
The setting prevents the JVM to shut down. after
System.out.println("Finished");
you still see the thread running with it's"Woke up"
log outputs.