Implementation of several termination methods of JVM

Source: Internet
Author: User
When Java .. shutdown is executed, the shutdown method in the shutdown class will be called: it will be called only after the last non-daemon thread is stopped. Not really stop 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 method: Execute hook and finalize. Note that there is no halt privatestatic void sequence () {synchronized (LOCK) {/* guard against thepossibility of a daemon thread invoking exit * afterdestroyjavavm initiates the shutdown sequence */==== if (State! = Hooks) return ;}runhooks () ;============= execute shutdown hooks Boolean rfoe; synchronized (LOCK) {state = finalizers; rfoe = runfinalizersonexit; }================ determine whether to execute finalize if (rfoe) runallfinalizers ();}

Run the command in system. Exit () mode: Call the exit (INT) method in runtime, and determine whether the operation may be performed according to the input parameter.Unallfinalizers, sequence, halt (Note: This includes 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);#################################        }    }

 Run kill, that is, call runtime. Halt (INT), shutdown. Halt (INT ).

/* 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);        }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.