为什么使用 RMI 时 JVM 不终止

发布于 2024-08-18 06:30:15 字数 1237 浏览 6 评论 0原文

我刚刚在 http://java.sun 上阅读了关于 RMI 的 Trail。 sun.com/docs/books/tutorial/rmi/implementing.html

当我运行该示例时,尽管 main 已完成,但 JVM 并未终止。 RMI 是否在内部某处生成线程?

main 退出后,main 中产生的多个线程的行为是什么? 这是一种让线程随时退出的干净方法,还是应该对生成的每个线程进行连接?我没有找到关于这个问题的任何文档。

非常感谢您的帮助!!

public class ComputeEngine implements Compute {

    public ComputeEngine() {
        super();
    }

    public <T> T executeTask(Task<T> t) {
        return t.execute();
    }


    public static void main(String[] args) {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Compute";
            Compute engine = new ComputeEngine();
            Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind(name, stub);
            System.out.println("ComputeEngine bound");
        } catch (Exception e) {
            System.err.println("ComputeEngine exception:");
            e.printStackTrace();
        }
    }
}

I just read the Trail on RMI from sun at http://java.sun.com/docs/books/tutorial/rmi/implementing.html

When I run the example, the JVM does not terminate although main has finished. Is RMI spawning a Thread somewhere internally?

What is the behaviour of multiple Threads spawned in main, after main exits?
Is it a clean way to let the Threads exit whenever they want or should you do a join on each Thread you spawn? I did not find any documentation on this question.

Thank you very much for your help!!

public class ComputeEngine implements Compute {

    public ComputeEngine() {
        super();
    }

    public <T> T executeTask(Task<T> t) {
        return t.execute();
    }


    public static void main(String[] args) {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Compute";
            Compute engine = new ComputeEngine();
            Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind(name, stub);
            System.out.println("ComputeEngine bound");
        } catch (Exception e) {
            System.err.println("ComputeEngine exception:");
            e.printStackTrace();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

如果没结果 2024-08-25 06:30:15

创建一个线程用于侦听套接字并回复对对象的请求。停止 JVM 的一种方法是取消绑定服务器:

Registry.unbind() 

并取消导出对象:

UnicastRemoteObject.unexportObject()). 

A thread is created for listening the socket and reply to requests to your object. One way to stop the JVM is to unbind the server:

Registry.unbind() 

and unexport the objects:

UnicastRemoteObject.unexportObject()). 
酒几许 2024-08-25 06:30:15

您可以使用 jstack 实用程序,包含在 JDK 中,以查看 java 程序中正在运行哪些线程,甚至这些线程位于哪一行。

因此,如果你的程序仍在运行,你只需在 pid 上运行 jstack,它就会告诉你哪些线程仍在运行以及它们正在做什么。

You can use the jstack utility program, included in the JDK to see which threads are running in a java program, and even what line those threads are on.

So if you're program is still running you just run jstack on the pid and it will tell you which threads are still running and what they're doing.

温柔少女心 2024-08-25 06:30:15

关于问题的“多线程行为”部分:是的,JVM 将继续运行,直到所有线程完成。唯一的例外是标记为守护进程的线程(请参阅 Thread.setDaemon()),JVM 不会等待它们。

Concerning the "behaviour of multiple threads" part of your question: yes, the JVM will keep on running until all threads finished. The only exception are threads marked as daemon (see Thread.setDaemon()), the JVM will not wait for them.

著墨染雨君画夕 2024-08-25 06:30:15

是的,当您通过 RMI 公开对象时,它需要一个线程来接受对这些对象的传入请求。该线程可能是一个守护线程,它不会阻止 JVM 退出,但由于多种原因,它不会阻止 JVM 退出,只要仍然存在活动的导出对象,它就会阻碍 JVM 正常退出。因此,您可以对所有对象使用 unexportObject 或仅使用 System.exit() 来结束 JVM,尽管这会让客户端不知道关闭情况。

Yes, when you are exposing objects through RMI it needs a thread to accept incoming requests for these objects. This thread could be a daemon thread which wouldn't stop the JVM from exiting but it isn't for several reasons and as long as there are still active exported objects it hinders the JVM from exiting normally. So you can use unexportObject for all objects or just use System.exit() to end the JVM although this would leave the clients uninformed about the shutdown.

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