77 * Terminates the currently running Java virtual machine by initiating its |
77 * Terminates the currently running Java virtual machine by initiating its |
78 * shutdown sequence. This method never returns normally. The argument |
78 * shutdown sequence. This method never returns normally. The argument |
79 * serves as a status code; by convention, a nonzero status code indicates |
79 * serves as a status code; by convention, a nonzero status code indicates |
80 * abnormal termination. |
80 * abnormal termination. |
81 * |
81 * |
82 * <p> The virtual machine's shutdown sequence consists of two phases. In |
82 * <p> All registered {@linkplain #addShutdownHook shutdown hooks}, if any, |
83 * the first phase all registered {@link #addShutdownHook shutdown hooks}, |
83 * are started in some unspecified order and allowed to run concurrently |
84 * if any, are started in some unspecified order and allowed to run |
84 * until they finish. Once this is done the virtual machine |
85 * concurrently until they finish. In the second phase all uninvoked |
85 * {@linkplain #halt halts}. |
86 * finalizers are run if {@link #runFinalizersOnExit finalization-on-exit} |
86 * |
87 * has been enabled. Once this is done the virtual machine {@link #halt halts}. |
87 * <p> If this method is invoked after all shutdown hooks have already |
88 * |
88 * been run and the status is nonzero then this method halts the |
89 * <p> If this method is invoked after the virtual machine has begun its |
89 * virtual machine with the given status code. Otherwise, this method |
90 * shutdown sequence then if shutdown hooks are being run this method will |
|
91 * block indefinitely. If shutdown hooks have already been run and on-exit |
|
92 * finalization has been enabled then this method halts the virtual machine |
|
93 * with the given status code if the status is nonzero; otherwise, it |
|
94 * blocks indefinitely. |
90 * blocks indefinitely. |
95 * |
91 * |
96 * <p> The {@link System#exit(int) System.exit} method is the |
92 * <p> The {@link System#exit(int) System.exit} method is the |
97 * conventional and convenient means of invoking this method. |
93 * conventional and convenient means of invoking this method. |
98 * |
94 * |
107 * |
103 * |
108 * @see java.lang.SecurityException |
104 * @see java.lang.SecurityException |
109 * @see java.lang.SecurityManager#checkExit(int) |
105 * @see java.lang.SecurityManager#checkExit(int) |
110 * @see #addShutdownHook |
106 * @see #addShutdownHook |
111 * @see #removeShutdownHook |
107 * @see #removeShutdownHook |
112 * @see #runFinalizersOnExit |
|
113 * @see #halt(int) |
108 * @see #halt(int) |
114 */ |
109 */ |
115 public void exit(int status) { |
110 public void exit(int status) { |
116 SecurityManager security = System.getSecurityManager(); |
111 SecurityManager security = System.getSecurityManager(); |
117 if (security != null) { |
112 if (security != null) { |
140 * |
135 * |
141 * <p> A <i>shutdown hook</i> is simply an initialized but unstarted |
136 * <p> A <i>shutdown hook</i> is simply an initialized but unstarted |
142 * thread. When the virtual machine begins its shutdown sequence it will |
137 * thread. When the virtual machine begins its shutdown sequence it will |
143 * start all registered shutdown hooks in some unspecified order and let |
138 * start all registered shutdown hooks in some unspecified order and let |
144 * them run concurrently. When all the hooks have finished it will then |
139 * them run concurrently. When all the hooks have finished it will then |
145 * run all uninvoked finalizers if finalization-on-exit has been enabled. |
140 * halt. Note that daemon threads will continue to run during the shutdown |
146 * Finally, the virtual machine will halt. Note that daemon threads will |
141 * sequence, as will non-daemon threads if shutdown was initiated by |
147 * continue to run during the shutdown sequence, as will non-daemon threads |
142 * invoking the {@link #exit exit} method. |
148 * if shutdown was initiated by invoking the {@link #exit exit} method. |
|
149 * |
143 * |
150 * <p> Once the shutdown sequence has begun it can be stopped only by |
144 * <p> Once the shutdown sequence has begun it can be stopped only by |
151 * invoking the {@link #halt halt} method, which forcibly |
145 * invoking the {@link #halt halt} method, which forcibly |
152 * terminates the virtual machine. |
146 * terminates the virtual machine. |
153 * |
147 * |
253 * Forcibly terminates the currently running Java virtual machine. This |
247 * Forcibly terminates the currently running Java virtual machine. This |
254 * method never returns normally. |
248 * method never returns normally. |
255 * |
249 * |
256 * <p> This method should be used with extreme caution. Unlike the |
250 * <p> This method should be used with extreme caution. Unlike the |
257 * {@link #exit exit} method, this method does not cause shutdown |
251 * {@link #exit exit} method, this method does not cause shutdown |
258 * hooks to be started and does not run uninvoked finalizers if |
252 * hooks to be started. If the shutdown sequence has already been |
259 * finalization-on-exit has been enabled. If the shutdown sequence has |
253 * initiated then this method does not wait for any running |
260 * already been initiated then this method does not wait for any running |
254 * shutdown hooks to finish their work. |
261 * shutdown hooks or finalizers to finish their work. |
|
262 * |
255 * |
263 * @param status |
256 * @param status |
264 * Termination status. By convention, a nonzero status code |
257 * Termination status. By convention, a nonzero status code |
265 * indicates abnormal termination. If the {@link Runtime#exit exit} |
258 * indicates abnormal termination. If the {@link Runtime#exit exit} |
266 * (equivalently, {@link System#exit(int) System.exit}) method |
259 * (equivalently, {@link System#exit(int) System.exit}) method |
281 SecurityManager sm = System.getSecurityManager(); |
274 SecurityManager sm = System.getSecurityManager(); |
282 if (sm != null) { |
275 if (sm != null) { |
283 sm.checkExit(status); |
276 sm.checkExit(status); |
284 } |
277 } |
285 Shutdown.halt(status); |
278 Shutdown.halt(status); |
286 } |
|
287 |
|
288 /** |
|
289 * Enable or disable finalization on exit; doing so specifies that the |
|
290 * finalizers of all objects that have finalizers that have not yet been |
|
291 * automatically invoked are to be run before the Java runtime exits. |
|
292 * By default, finalization on exit is disabled. |
|
293 * |
|
294 * <p>If there is a security manager, |
|
295 * its {@code checkExit} method is first called |
|
296 * with 0 as its argument to ensure the exit is allowed. |
|
297 * This could result in a SecurityException. |
|
298 * |
|
299 * @param value true to enable finalization on exit, false to disable |
|
300 * @deprecated This method is inherently unsafe. It may result in |
|
301 * finalizers being called on live objects while other threads are |
|
302 * concurrently manipulating those objects, resulting in erratic |
|
303 * behavior or deadlock. |
|
304 * This method is subject to removal in a future version of Java SE. |
|
305 * |
|
306 * @throws SecurityException |
|
307 * if a security manager exists and its {@code checkExit} |
|
308 * method doesn't allow the exit. |
|
309 * |
|
310 * @see java.lang.Runtime#exit(int) |
|
311 * @see java.lang.Runtime#gc() |
|
312 * @see java.lang.SecurityManager#checkExit(int) |
|
313 * @since 1.1 |
|
314 */ |
|
315 @Deprecated(since="1.2", forRemoval=true) |
|
316 public static void runFinalizersOnExit(boolean value) { |
|
317 SecurityManager security = System.getSecurityManager(); |
|
318 if (security != null) { |
|
319 try { |
|
320 security.checkExit(0); |
|
321 } catch (SecurityException e) { |
|
322 throw new SecurityException("runFinalizersOnExit"); |
|
323 } |
|
324 } |
|
325 Shutdown.setRunFinalizersOnExit(value); |
|
326 } |
279 } |
327 |
280 |
328 /** |
281 /** |
329 * Executes the specified string command in a separate process. |
282 * Executes the specified string command in a separate process. |
330 * |
283 * |