Java JVM在系统中生存。退出(1)

Java JVM在系统中生存。退出(1),java,Java,我目前正在使用system.exit(1)维护一个在发生故障时关闭的系统。不幸的是,发生这种情况时,系统的java进程仍然存在 调用system.exit时jvm怎么可能不关闭?一种可能是调用system.exit(…)会抛出安全异常,这是因为system.exit(1)不会关闭挂钩,请使用system.exit(0) 实际上,根据以下代码,系统.exit(1)执行“暂停和停止”: /* Invoked by Runtime.exit, which does all the security c

我目前正在使用system.exit(1)维护一个在发生故障时关闭的系统。不幸的是,发生这种情况时,系统的java进程仍然存在


调用system.exit时jvm怎么可能不关闭?

一种可能是调用
system.exit(…)
会抛出
安全异常
,这是因为
system.exit(1)
不会关闭挂钩,请使用
system.exit(0)

实际上,根据以下代码,
系统.exit(1)
执行“暂停和停止”:

/* Invoked by Runtime.exit, which does all the security checks.
 * Also invoked by handlers for system-provided termination events,
 * which should pass a nonzero status code.
 */
static void exit(int status) {
boolean runMoreFinalizers = false;
synchronized (lock) {
    if (status != 0) runFinalizersOnExit = false;
    switch (state) {
    case RUNNING:   /* Initiate shutdown */
    state = HOOKS;
    break;
    case HOOKS:     /* Stall and halt */
    break;
    case FINALIZERS:
    if (status != 0) {
        /* Halt immediately on nonzero status */
        halt(status);
    } else {
        /* Compatibility with old behavior:
         * Run more finalizers and then halt
         */
        runMoreFinalizers = runFinalizersOnExit;
    }
    break;
    }
}
if (runMoreFinalizers) {
    runAllFinalizers();
    halt(status);
}
synchronized (Shutdown.class) {
    /* Synchronize on the class object, causing any other thread
         * that attempts to initiate shutdown to stall indefinitely
     */
    sequence();
    halt(status);
}
}

有关可能的解决方案,请参见。这个想法基本上是在单独的线程中触发System.exit,而不是在主线程中。

我不明白这个答案。OP的问题不是关闭挂钩没有运行。这是因为JVM没有退出。这非常有用,我不知道有一些奇怪的钩子阻止了关闭。