是什么让rmi服务器继续运行?

发布于 2024-12-05 05:50:45 字数 603 浏览 0 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帝王念 2024-12-12 05:50:45

让它继续运行的是一个由 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().

时光瘦了 2024-12-12 05:50:45

要停止它,你应该打电话

LocateRegistry.getRegistry().unbind("rmi://host:port/name");

To stop it, you should call

LocateRegistry.getRegistry().unbind("rmi://host:port/name");
回眸一遍 2024-12-12 05:50:45

但我不明白的是,是什么让该程序继续运行,而它不是像 while(true){}; 这样的东西?除了停止整个程序之外,如何阻止它监听?

这是由编辑-编辑守护线程完成的。请参阅:Java 中的守护线程是什么?
您可以使用这个小示例来测试行为:

public class DaemonThread extends Thread
{
  public void run(){
    System.out.println("Entering run method");
    try
    {
      System.out.println(Thread.currentThread());
      while (true)
      {
        try {Thread.sleep(500);}
        catch (InterruptedException x) {}
        System.out.println("Woke up");
      }
    }
    finally { System.out.println("run finished");}
  }

  public static void main(String[] args) throws InterruptedException{
    System.out.println("Main");
    DaemonThread t = new DaemonThread();
    t.setDaemon(false);  // Set to true for testing
    t.start();
    Thread.sleep(2000);
    System.out.println("Finished");
  }
}

该设置阻止 JVM 关闭。在 System.out.println("Finished"); 之后,您仍然会看到线程正在运行,并带有它的 "Woke up" 日志输出。

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?

This is done by a edit non-editdaemon thread. See: What is Daemon thread in Java?
You can test the behavior with this little example:

public class DaemonThread extends Thread
{
  public void run(){
    System.out.println("Entering run method");
    try
    {
      System.out.println(Thread.currentThread());
      while (true)
      {
        try {Thread.sleep(500);}
        catch (InterruptedException x) {}
        System.out.println("Woke up");
      }
    }
    finally { System.out.println("run finished");}
  }

  public static void main(String[] args) throws InterruptedException{
    System.out.println("Main");
    DaemonThread t = new DaemonThread();
    t.setDaemon(false);  // Set to true for testing
    t.start();
    Thread.sleep(2000);
    System.out.println("Finished");
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文