这是使用RMI的客户端/服务器应用程序。服务器接收到针对destroy()的RMI请求后-我需要中止VM。我为此使用Runtime.getRuntime().halt(0)。但是在调用halt()之后,VM仍然存在,而我唯一可以杀死它的方法是在OSX上使用Force Kill

调用线程的堆栈跟踪如下:

"RMI TCP Connection(3)-192.168.1.4" daemon prio=5 tid=7f99a7103000 nid=0x11d467000 runnable [11d464000]
   java.lang.Thread.State: RUNNABLE
    at java.lang.Shutdown.halt0(Native Method)
    at java.lang.Shutdown.halt(Shutdown.java:95)
    - locked <7f44c90d0> (a java.lang.Shutdown$Lock)
    at java.lang.Runtime.halt(Runtime.java:256)
    at net.sourceforge.marathon.runtime.JavaRuntime.destroy(JavaRuntime.java:178)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at net.sourceforge.rmilite.impl.RemoteInvocationHandlerImpl.invoke(RemoteInvocationHandlerImpl.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303)
    at sun.rmi.transport.Transport$1.run(Transport.java:159)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)


可能是什么问题?

最佳答案

为了使这项工作,我尝试了多种方法。


在调用halt()之前添加了Runtime#runFinalization
halt()中调用了SwingUtilities.invokeAndWait
halt()中调用了SwingUtilities.invokeLater


这些都不起作用。最后,


使用halt()在计时器任务中调用Timer#schedule


这一直有效,并且一直持续到现在。感谢大家的答复。

关于java - Java Runtime#halt()不会停止JVM,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14044061/

10-10 10:47