jvm的几种终止方式的实现

来源:互联网 发布:java urlencode 编辑:程序博客网 时间:2024/05/17 04:47

在执行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);        }}