笨笨
独学而无友,则孤陋而寡闻
今天去Tomcat代码里面查东西,无意中发现:
 1     /**
 2      * Start a new server instance.
 3      */
 4     public void start() {
 5 
 6         if (server == null) {
 7             load();
 8         }
 9 
10         long t1 = System.currentTimeMillis();
11 
12         // Start the new server
13         if (server instanceof Lifecycle) {
14             try {
15                 ((Lifecycle) server).start();
16             } catch (LifecycleException e) {
17                 log.error("Catalina.start: ", e);
18             }
19         }
20 
21         long t2 = System.currentTimeMillis();
22         log.info("Server startup in " + (t2 - t1) + " ms");
23 
24         try {
25             // Register shutdown hook
26             if (useShutdownHook) {
27                 if (shutdownHook == null) {
28                     shutdownHook = new CatalinaShutdownHook();
29                 }
30                 Runtime.getRuntime().addShutdownHook(shutdownHook);
31             }
32         } catch (Throwable t) {
33             // This will fail on JDK 1.2. Ignoring, as Tomcat can run
34             // fine without the shutdown hook.
35         }
36 
37         if (await) {
38             await();
39             stop();
40         }
41 
42     }

其中:

 1     /**
 2      * Shutdown hook which will perform a clean shutdown of Catalina if needed.
 3      */
 4     protected class CatalinaShutdownHook extends Thread {
 5 
 6         public void run() {
 7 
 8             if (server != null) {
 9                 Catalina.this.stop();
10             }
11             
12         }
13 
14     }


而:
 1   /**
 2      * Registers a new virtual-machine shutdown hook.
 3      *
 4      * <p> The Java virtual machine <i>shuts down</i> in response to two kinds
 5      * of events:
 6      *
 7      *   <ul>
 8      *
 9      *   <p> <li> The program <i>exits</i> normally, when the last non-daemon
10      *   thread exits or when the <tt>{@link #exit exit}</tt> (equivalently,
11      *   <tt>{@link System#exit(int) System.exit}</tt>) method is invoked, or
12      *
13      *   <p> <li> The virtual machine is <i>terminated</i> in response to a
14      *   user interrupt, such as typing <tt>^C</tt>, or a system-wide event,
15      *   such as user logoff or system shutdown.
16      *
17      *   </ul>
18      *
19      * <p> A <i>shutdown hook</i> is simply an initialized but unstarted
20      * thread.  When the virtual machine begins its shutdown sequence it will
21      * start all registered shutdown hooks in some unspecified order and let
22      * them run concurrently.  When all the hooks have finished it will then
23      * run all uninvoked finalizers if finalization-on-exit has been enabled.
24      * Finally, the virtual machine will halt.  Note that daemon threads will
25      * continue to run during the shutdown sequence, as will non-daemon threads
26      * if shutdown was initiated by invoking the <tt>{@link #exit exit}</tt>
27      * method.
28      *
29      * <p> Once the shutdown sequence has begun it can be stopped only by
30      * invoking the <tt>{@link #halt halt}</tt> method, which forcibly
31      * terminates the virtual machine.
32      *
33      * <p> Once the shutdown sequence has begun it is impossible to register a
34      * new shutdown hook or de-register a previously-registered hook.
35      * Attempting either of these operations will cause an
36      * <tt>{@link IllegalStateException}</tt> to be thrown.
37      *
38      * <p> Shutdown hooks run at a delicate time in the life cycle of a virtual
39      * machine and should therefore be coded defensively.  They should, in
40      * particular, be written to be thread-safe and to avoid deadlocks insofar
41      * as possible.  They should also not rely blindly upon services that may
42      * have registered their own shutdown hooks and therefore may themselves in
43      * the process of shutting down.
44      *
45      * <p> Shutdown hooks should also finish their work quickly.  When a
46      * program invokes <tt>{@link #exit exit}</tt> the expectation is
47      * that the virtual machine will promptly shut down and exit.  When the
48      * virtual machine is terminated due to user logoff or system shutdown the
49      * underlying operating system may only allow a fixed amount of time in
50      * which to shut down and exit.  It is therefore inadvisable to attempt any
51      * user interaction or to perform a long-running computation in a shutdown
52      * hook.
53      *
54      * <p> Uncaught exceptions are handled in shutdown hooks just as in any
55      * other thread, by invoking the <tt>{@link ThreadGroup#uncaughtException
56      * uncaughtException}</tt> method of the thread's <tt>{@link
57      * ThreadGroup}</tt> object.  The default implementation of this method
58      * prints the exception's stack trace to <tt>{@link System#err}</tt> and
59      * terminates the thread; it does not cause the virtual machine to exit or
60      * halt.
61      *
62      * <p> In rare circumstances the virtual machine may <i>abort</i>, that is,
63      * stop running without shutting down cleanly.  This occurs when the
64      * virtual machine is terminated externally, for example with the
65      * <tt>SIGKILL</tt> signal on Unix or the <tt>TerminateProcess</tt> call on
66      * Microsoft Windows.  The virtual machine may also abort if a native method goes awry
67      * by, for example, corrupting internal data structures or attempting to
68      * access nonexistent memory.  If the virtual machine aborts then no
69      * guarantee can be made about whether or not any shutdown hooks will be
70      * run. <p>
71      *
72      * @param   hook
73      *          An initialized but unstarted <tt>{@link Thread}</tt> object
74      *
75      * @throws  IllegalArgumentException
76      *          If the specified hook has already been registered,
77      *          or if it can be determined that the hook is already running or
78      *          has already been run
79      *
80      * @throws  IllegalStateException
81      *          If the virtual machine is already in the process
82      *          of shutting down
83      *
84      * @throws  SecurityException
85      *          If a security manager is present and it denies
86      *          <tt>{@link RuntimePermission}("shutdownHooks")</tt>
87      *
88      * @see #removeShutdownHook
89      * @see #halt(int)
90      * @see #exit(int)
91      * @since 1.3
92      */
93     public void addShutdownHook(Thread hook) {
94     SecurityManager sm = System.getSecurityManager();
95     if (sm != null) {
96         sm.checkPermission(new RuntimePermission("shutdownHooks"));
97     }
98     Shutdown.add(hook);
99     }

再来看看Shutdow:
 1     /* Add a new shutdown hook.  Checks the shutdown state and the hook itself,
 2      * but does not do any security checks.
 3      */
 4     static void add(Thread hook) {
 5     synchronized (lock) {
 6         if (state > RUNNING)
 7         throw new IllegalStateException("Shutdown in progress");
 8         if (hook.isAlive())
 9         throw new IllegalArgumentException("Hook already running");
10         if (hooks == null) {
11         hooks = new HashSet(11);
12         hooks.add(new WrappedHook(hook));
13         Terminator.setup();
14         } else {
15         WrappedHook wh = new WrappedHook(hook);
16         if (hooks.contains(wh))
17             throw new IllegalArgumentException("Hook previously registered");
18         hooks.add(wh);
19         }
20     }
21     }


结论:
还是好多东西以前不知道,想起那个怪圈来了。
posted on 2005-11-11 16:47  笨笨  阅读(613)  评论(0编辑  收藏  举报