jvm的几种终止方式的实现

本文深入探讨了Java JVM在关闭过程中的工作机制,包括如何触发关闭流程,不同关闭阶段的操作,以及通过系统退出和kill指令实现关闭的具体实现细节。

在执行java .. shutdown的时候,会调用到ShutDown类中的shutdown方法:在最后一个non-daemon thread停掉之后才会调用到。不是真正的停掉jvm。

 

/* Invoked by the JNI DestroyJavaVM procedure whenthe last non-daemon
    * thread has finished. Unlike the exit method, this methoddoes not
     *actually halt the VM.
    */
   static void shutdown() {
        synchronized (lock) {
            switch (state) {
            case RUNNING:   /* Initiate shutdown */
                  state = HOOKS;
                  break;
            case HOOKS:                 /* Stall and then return */
            case FINALIZERS:
                  break;
            }
        }
        synchronized (Shutdown.class) {
            sequence();
        }
}
 
Sequece方法:执行hook和Finalize,注意,没有halt
 
    privatestatic void sequence() {
     synchronized (lock) {
         /* Guard against thepossibility of a daemon thread invoking exit
          * afterDestroyJavaVM initiates the shutdown sequence
          */====不允许在daemon进程中调用
         if (state != HOOKS)return;
     }
    runHooks();       =============执行shutdown hooks
     boolean rfoe;
     synchronized (lock) {
         state = FINALIZERS;
         rfoe =runFinalizersOnExit;
     }
 ==============判断是不是要执行Finalize
     if (rfoe) runAllFinalizers();
    }

通过system.exit()方式执行:调用到runtime中的exit(int)方法,根据传入参数判断可能执行到unAllFinalizers,sequence,halt(注意,这里面包含halt的)


static void exit(intstatus) {
        boolean runMoreFinalizers = false;
        synchronized (lock) {
            if (status != 0) runFinalizersOnExit = false;
            switch (state) {
            case RUNNING:   /* Initiate shutdown */
                  state = HOOKS;
                  break;
            caseHOOKS:                 /* 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);#################################
        }
    }

 通过kill执行,也就是相当于调用runtime.halt(int),shutdown.halt(int),二话不说,直接调用native方法halt掉

/* The halt method is synchronized on the halt lock
     * to avoid corruption of the delete-on-shutdown file list.
     * It invokes the true native halt method.
     */
    static void halt(int status) {
        synchronized (haltLock) {
            halt0(status);
        }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值