< prev index next >

src/share/classes/java/lang/Shutdown.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 113     private static void runHooks() {
 114         for (int i=0; i < MAX_SYSTEM_HOOKS; i++) {
 115             try {
 116                 Runnable hook;
 117                 synchronized (lock) {
 118                     // acquire the lock to make sure the hook registered during
 119                     // shutdown is visible here.
 120                     currentRunningHook = i;
 121                     hook = hooks[i];
 122                 }
 123                 if (hook != null) hook.run();
 124             } catch(Throwable t) {
 125                 if (t instanceof ThreadDeath) {
 126                     ThreadDeath td = (ThreadDeath)t;
 127                     throw td;
 128                 }
 129             }
 130         }
 131     }
 132 



 133     /* The halt method is synchronized on the halt lock
 134      * to avoid corruption of the delete-on-shutdown file list.
 135      * It invokes the true native halt method.
 136      */
 137     static void halt(int status) {
 138         synchronized (haltLock) {
 139             halt0(status);
 140         }
 141     }
 142 
 143     static native void halt0(int status);
 144 
 145     /* Wormhole for invoking java.lang.ref.Finalizer.runAllFinalizers */
 146     private static native void runAllFinalizers();
 147 
 148 
 149     /* The actual shutdown sequence is defined here.
 150      *
 151      * If it weren't for runFinalizersOnExit, this would be simple -- we'd just
 152      * run the hooks and then halt.  Instead we need to keep track of whether


 192                 if (status != 0) {
 193                     /* Halt immediately on nonzero status */
 194                     halt(status);
 195                 } else {
 196                     /* Compatibility with old behavior:
 197                      * Run more finalizers and then halt
 198                      */
 199                     runMoreFinalizers = runFinalizersOnExit;
 200                 }
 201                 break;
 202             }
 203         }
 204         if (runMoreFinalizers) {
 205             runAllFinalizers();
 206             halt(status);
 207         }
 208         synchronized (Shutdown.class) {
 209             /* Synchronize on the class object, causing any other thread
 210              * that attempts to initiate shutdown to stall indefinitely
 211              */

 212             sequence();
 213             halt(status);
 214         }
 215     }
 216 
 217 
 218     /* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon
 219      * thread has finished.  Unlike the exit method, this method does not
 220      * actually halt the VM.
 221      */
 222     static void shutdown() {
 223         synchronized (lock) {
 224             switch (state) {
 225             case RUNNING:       /* Initiate shutdown */
 226                 state = HOOKS;
 227                 break;
 228             case HOOKS:         /* Stall and then return */
 229             case FINALIZERS:
 230                 break;
 231             }
   1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 113     private static void runHooks() {
 114         for (int i=0; i < MAX_SYSTEM_HOOKS; i++) {
 115             try {
 116                 Runnable hook;
 117                 synchronized (lock) {
 118                     // acquire the lock to make sure the hook registered during
 119                     // shutdown is visible here.
 120                     currentRunningHook = i;
 121                     hook = hooks[i];
 122                 }
 123                 if (hook != null) hook.run();
 124             } catch(Throwable t) {
 125                 if (t instanceof ThreadDeath) {
 126                     ThreadDeath td = (ThreadDeath)t;
 127                     throw td;
 128                 }
 129             }
 130         }
 131     }
 132 
 133     /* Notify the VM that it's time to halt. */
 134     static native void beforeHalt();
 135 
 136     /* The halt method is synchronized on the halt lock
 137      * to avoid corruption of the delete-on-shutdown file list.
 138      * It invokes the true native halt method.
 139      */
 140     static void halt(int status) {
 141         synchronized (haltLock) {
 142             halt0(status);
 143         }
 144     }
 145 
 146     static native void halt0(int status);
 147 
 148     /* Wormhole for invoking java.lang.ref.Finalizer.runAllFinalizers */
 149     private static native void runAllFinalizers();
 150 
 151 
 152     /* The actual shutdown sequence is defined here.
 153      *
 154      * If it weren't for runFinalizersOnExit, this would be simple -- we'd just
 155      * run the hooks and then halt.  Instead we need to keep track of whether


 195                 if (status != 0) {
 196                     /* Halt immediately on nonzero status */
 197                     halt(status);
 198                 } else {
 199                     /* Compatibility with old behavior:
 200                      * Run more finalizers and then halt
 201                      */
 202                     runMoreFinalizers = runFinalizersOnExit;
 203                 }
 204                 break;
 205             }
 206         }
 207         if (runMoreFinalizers) {
 208             runAllFinalizers();
 209             halt(status);
 210         }
 211         synchronized (Shutdown.class) {
 212             /* Synchronize on the class object, causing any other thread
 213              * that attempts to initiate shutdown to stall indefinitely
 214              */
 215             beforeHalt();
 216             sequence();
 217             halt(status);
 218         }
 219     }
 220 
 221 
 222     /* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon
 223      * thread has finished.  Unlike the exit method, this method does not
 224      * actually halt the VM.
 225      */
 226     static void shutdown() {
 227         synchronized (lock) {
 228             switch (state) {
 229             case RUNNING:       /* Initiate shutdown */
 230                 state = HOOKS;
 231                 break;
 232             case HOOKS:         /* Stall and then return */
 233             case FINALIZERS:
 234                 break;
 235             }
< prev index next >