Source Code Cross Referenced for ApplicationShutdownHooks.java in  » JDK-Core » lang » java » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. JDK Core
2. JDK Modules
3. JDK Modules com.sun
4. JDK Modules com.sun.java
5. JDK Modules Platform
6. JDK Modules sun
7. Open Source Build
8. Open Source Graphic Library
9. Open Source IDE Eclipse
10. Open Source J2EE
11. Open Source JDBC Driver
12. Open Source Library
13. Open Source Library Database
14. Open Source Net
15. Open Source Script
16. Science
17. Security
18. Sevlet Container
19. SUN GlassFish
20. Swing Library
21. Web Services apache cxf 2.0.1
22. Web Services AXIS2
23. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Source Code / Java Documentation » JDK Core » lang » java.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:        package java.lang;
026:
027:        import java.util.*;
028:
029:        /*
030:         * Class to track and run user level shutdown hooks registered through
031:         * <tt>{@link Runtime#addShutdownHook Runtime.addShutdownHook}</tt>.
032:         *
033:         * @see java.lang.Runtime#addShutdownHook
034:         * @see java.lang.Runtime#removeShutdownHook
035:         */
036:
037:        class ApplicationShutdownHooks implements  Runnable {
038:            private static ApplicationShutdownHooks instance = null;
039:
040:            /* The set of registered hooks */
041:            private static IdentityHashMap<Thread, Thread> hooks = new IdentityHashMap<Thread, Thread>();
042:
043:            static synchronized ApplicationShutdownHooks hook() {
044:                if (instance == null)
045:                    instance = new ApplicationShutdownHooks();
046:
047:                return instance;
048:            }
049:
050:            private ApplicationShutdownHooks() {
051:            }
052:
053:            /* Add a new shutdown hook.  Checks the shutdown state and the hook itself,
054:             * but does not do any security checks.
055:             */
056:            static synchronized void add(Thread hook) {
057:                if (hooks == null)
058:                    throw new IllegalStateException("Shutdown in progress");
059:
060:                if (hook.isAlive())
061:                    throw new IllegalArgumentException("Hook already running");
062:
063:                if (hooks.containsKey(hook))
064:                    throw new IllegalArgumentException(
065:                            "Hook previously registered");
066:
067:                hooks.put(hook, hook);
068:            }
069:
070:            /* Remove a previously-registered hook.  Like the add method, this method
071:             * does not do any security checks.
072:             */
073:            static synchronized boolean remove(Thread hook) {
074:                if (hooks == null)
075:                    throw new IllegalStateException("Shutdown in progress");
076:
077:                if (hook == null)
078:                    throw new NullPointerException();
079:
080:                return hooks.remove(hook) != null;
081:            }
082:
083:            /* Iterates over all application hooks creating a new thread for each
084:             * to run in. Hooks are run concurrently and this method waits for 
085:             * them to finish.
086:             */
087:            public void run() {
088:                Collection<Thread> threads;
089:                synchronized (ApplicationShutdownHooks.class) {
090:                    threads = hooks.keySet();
091:                    hooks = null;
092:                }
093:
094:                for (Thread hook : threads) {
095:                    hook.start();
096:                }
097:                for (Thread hook : threads) {
098:                    try {
099:                        hook.join();
100:                    } catch (InterruptedException x) {
101:                    }
102:                }
103:            }
104:        }
w__ww__.j___ava2___s___.__c_om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.