src/share/classes/java/lang/Runtime.java

Print this page




  66      * shutdown sequence.  This method never returns normally.  The argument
  67      * serves as a status code; by convention, a nonzero status code indicates
  68      * abnormal termination.
  69      *
  70      * <p> The virtual machine's shutdown sequence consists of two phases.  In
  71      * the first phase all registered {@link #addShutdownHook shutdown hooks},
  72      * if any, are started in some unspecified order and allowed to run
  73      * concurrently until they finish.  In the second phase all uninvoked
  74      * finalizers are run if {@link #runFinalizersOnExit finalization-on-exit}
  75      * has been enabled.  Once this is done the virtual machine {@link #halt
  76      * halts}.
  77      *
  78      * <p> If this method is invoked after the virtual machine has begun its
  79      * shutdown sequence then if shutdown hooks are being run this method will
  80      * block indefinitely.  If shutdown hooks have already been run and on-exit
  81      * finalization has been enabled then this method halts the virtual machine
  82      * with the given status code if the status is nonzero; otherwise, it
  83      * blocks indefinitely.
  84      *
  85      * <p> The <tt>{@link System#exit(int) System.exit}</tt> method is the
  86      * conventional and convenient means of invoking this method. <p>
  87      *
  88      * @param  status
  89      *         Termination status.  By convention, a nonzero status code
  90      *         indicates abnormal termination.
  91      *
  92      * @throws SecurityException
  93      *         If a security manager is present and its <tt>{@link
  94      *         SecurityManager#checkExit checkExit}</tt> method does not permit
  95      *         exiting with the specified status
  96      *
  97      * @see java.lang.SecurityException
  98      * @see java.lang.SecurityManager#checkExit(int)
  99      * @see #addShutdownHook
 100      * @see #removeShutdownHook
 101      * @see #runFinalizersOnExit
 102      * @see #halt(int)
 103      */
 104     public void exit(int status) {
 105         SecurityManager security = System.getSecurityManager();
 106         if (security != null) {


 163      * which to shut down and exit.  It is therefore inadvisable to attempt any
 164      * user interaction or to perform a long-running computation in a shutdown
 165      * hook.
 166      *
 167      * <p> Uncaught exceptions are handled in shutdown hooks just as in any
 168      * other thread, by invoking the <tt>{@link ThreadGroup#uncaughtException
 169      * uncaughtException}</tt> method of the thread's <tt>{@link
 170      * ThreadGroup}</tt> object.  The default implementation of this method
 171      * prints the exception's stack trace to <tt>{@link System#err}</tt> and
 172      * terminates the thread; it does not cause the virtual machine to exit or
 173      * halt.
 174      *
 175      * <p> In rare circumstances the virtual machine may <i>abort</i>, that is,
 176      * stop running without shutting down cleanly.  This occurs when the
 177      * virtual machine is terminated externally, for example with the
 178      * <tt>SIGKILL</tt> signal on Unix or the <tt>TerminateProcess</tt> call on
 179      * Microsoft Windows.  The virtual machine may also abort if a native
 180      * method goes awry by, for example, corrupting internal data structures or
 181      * attempting to access nonexistent memory.  If the virtual machine aborts
 182      * then no guarantee can be made about whether or not any shutdown hooks
 183      * will be run. <p>
 184      *
 185      * @param   hook
 186      *          An initialized but unstarted <tt>{@link Thread}</tt> object
 187      *
 188      * @throws  IllegalArgumentException
 189      *          If the specified hook has already been registered,
 190      *          or if it can be determined that the hook is already running or
 191      *          has already been run
 192      *
 193      * @throws  IllegalStateException
 194      *          If the virtual machine is already in the process
 195      *          of shutting down
 196      *
 197      * @throws  SecurityException
 198      *          If a security manager is present and it denies
 199      *          <tt>{@link RuntimePermission}("shutdownHooks")</tt>
 200      *
 201      * @see #removeShutdownHook
 202      * @see #halt(int)
 203      * @see #exit(int)


 231      * @see #exit(int)
 232      * @since 1.3
 233      */
 234     public boolean removeShutdownHook(Thread hook) {
 235         SecurityManager sm = System.getSecurityManager();
 236         if (sm != null) {
 237             sm.checkPermission(new RuntimePermission("shutdownHooks"));
 238         }
 239         return ApplicationShutdownHooks.remove(hook);
 240     }
 241 
 242     /**
 243      * Forcibly terminates the currently running Java virtual machine.  This
 244      * method never returns normally.
 245      *
 246      * <p> This method should be used with extreme caution.  Unlike the
 247      * <tt>{@link #exit exit}</tt> method, this method does not cause shutdown
 248      * hooks to be started and does not run uninvoked finalizers if
 249      * finalization-on-exit has been enabled.  If the shutdown sequence has
 250      * already been initiated then this method does not wait for any running
 251      * shutdown hooks or finalizers to finish their work. <p>
 252      *
 253      * @param  status
 254      *         Termination status.  By convention, a nonzero status code
 255      *         indicates abnormal termination.  If the <tt>{@link Runtime#exit
 256      *         exit}</tt> (equivalently, <tt>{@link System#exit(int)
 257      *         System.exit}</tt>) method has already been invoked then this
 258      *         status code will override the status code passed to that method.
 259      *
 260      * @throws SecurityException
 261      *         If a security manager is present and its <tt>{@link
 262      *         SecurityManager#checkExit checkExit}</tt> method does not permit
 263      *         an exit with the specified status
 264      *
 265      * @see #exit
 266      * @see #addShutdownHook
 267      * @see #removeShutdownHook
 268      * @since 1.3
 269      */
 270     public void halt(int status) {
 271         SecurityManager sm = System.getSecurityManager();




  66      * shutdown sequence.  This method never returns normally.  The argument
  67      * serves as a status code; by convention, a nonzero status code indicates
  68      * abnormal termination.
  69      *
  70      * <p> The virtual machine's shutdown sequence consists of two phases.  In
  71      * the first phase all registered {@link #addShutdownHook shutdown hooks},
  72      * if any, are started in some unspecified order and allowed to run
  73      * concurrently until they finish.  In the second phase all uninvoked
  74      * finalizers are run if {@link #runFinalizersOnExit finalization-on-exit}
  75      * has been enabled.  Once this is done the virtual machine {@link #halt
  76      * halts}.
  77      *
  78      * <p> If this method is invoked after the virtual machine has begun its
  79      * shutdown sequence then if shutdown hooks are being run this method will
  80      * block indefinitely.  If shutdown hooks have already been run and on-exit
  81      * finalization has been enabled then this method halts the virtual machine
  82      * with the given status code if the status is nonzero; otherwise, it
  83      * blocks indefinitely.
  84      *
  85      * <p> The <tt>{@link System#exit(int) System.exit}</tt> method is the
  86      * conventional and convenient means of invoking this method.
  87      *
  88      * @param  status
  89      *         Termination status.  By convention, a nonzero status code
  90      *         indicates abnormal termination.
  91      *
  92      * @throws SecurityException
  93      *         If a security manager is present and its <tt>{@link
  94      *         SecurityManager#checkExit checkExit}</tt> method does not permit
  95      *         exiting with the specified status
  96      *
  97      * @see java.lang.SecurityException
  98      * @see java.lang.SecurityManager#checkExit(int)
  99      * @see #addShutdownHook
 100      * @see #removeShutdownHook
 101      * @see #runFinalizersOnExit
 102      * @see #halt(int)
 103      */
 104     public void exit(int status) {
 105         SecurityManager security = System.getSecurityManager();
 106         if (security != null) {


 163      * which to shut down and exit.  It is therefore inadvisable to attempt any
 164      * user interaction or to perform a long-running computation in a shutdown
 165      * hook.
 166      *
 167      * <p> Uncaught exceptions are handled in shutdown hooks just as in any
 168      * other thread, by invoking the <tt>{@link ThreadGroup#uncaughtException
 169      * uncaughtException}</tt> method of the thread's <tt>{@link
 170      * ThreadGroup}</tt> object.  The default implementation of this method
 171      * prints the exception's stack trace to <tt>{@link System#err}</tt> and
 172      * terminates the thread; it does not cause the virtual machine to exit or
 173      * halt.
 174      *
 175      * <p> In rare circumstances the virtual machine may <i>abort</i>, that is,
 176      * stop running without shutting down cleanly.  This occurs when the
 177      * virtual machine is terminated externally, for example with the
 178      * <tt>SIGKILL</tt> signal on Unix or the <tt>TerminateProcess</tt> call on
 179      * Microsoft Windows.  The virtual machine may also abort if a native
 180      * method goes awry by, for example, corrupting internal data structures or
 181      * attempting to access nonexistent memory.  If the virtual machine aborts
 182      * then no guarantee can be made about whether or not any shutdown hooks
 183      * will be run.
 184      *
 185      * @param   hook
 186      *          An initialized but unstarted <tt>{@link Thread}</tt> object
 187      *
 188      * @throws  IllegalArgumentException
 189      *          If the specified hook has already been registered,
 190      *          or if it can be determined that the hook is already running or
 191      *          has already been run
 192      *
 193      * @throws  IllegalStateException
 194      *          If the virtual machine is already in the process
 195      *          of shutting down
 196      *
 197      * @throws  SecurityException
 198      *          If a security manager is present and it denies
 199      *          <tt>{@link RuntimePermission}("shutdownHooks")</tt>
 200      *
 201      * @see #removeShutdownHook
 202      * @see #halt(int)
 203      * @see #exit(int)


 231      * @see #exit(int)
 232      * @since 1.3
 233      */
 234     public boolean removeShutdownHook(Thread hook) {
 235         SecurityManager sm = System.getSecurityManager();
 236         if (sm != null) {
 237             sm.checkPermission(new RuntimePermission("shutdownHooks"));
 238         }
 239         return ApplicationShutdownHooks.remove(hook);
 240     }
 241 
 242     /**
 243      * Forcibly terminates the currently running Java virtual machine.  This
 244      * method never returns normally.
 245      *
 246      * <p> This method should be used with extreme caution.  Unlike the
 247      * <tt>{@link #exit exit}</tt> method, this method does not cause shutdown
 248      * hooks to be started and does not run uninvoked finalizers if
 249      * finalization-on-exit has been enabled.  If the shutdown sequence has
 250      * already been initiated then this method does not wait for any running
 251      * shutdown hooks or finalizers to finish their work.
 252      *
 253      * @param  status
 254      *         Termination status.  By convention, a nonzero status code
 255      *         indicates abnormal termination.  If the <tt>{@link Runtime#exit
 256      *         exit}</tt> (equivalently, <tt>{@link System#exit(int)
 257      *         System.exit}</tt>) method has already been invoked then this
 258      *         status code will override the status code passed to that method.
 259      *
 260      * @throws SecurityException
 261      *         If a security manager is present and its <tt>{@link
 262      *         SecurityManager#checkExit checkExit}</tt> method does not permit
 263      *         an exit with the specified status
 264      *
 265      * @see #exit
 266      * @see #addShutdownHook
 267      * @see #removeShutdownHook
 268      * @since 1.3
 269      */
 270     public void halt(int status) {
 271         SecurityManager sm = System.getSecurityManager();