< prev index next >

src/java.base/share/classes/java/lang/ApplicationShutdownHooks.java

Print this page

  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
 23  * questions.
 24  */
 25 package java.lang;
 26 
 27 import java.util.*;

 28 
 29 /*
 30  * Class to track and run user level shutdown hooks registered through
 31  * {@link Runtime#addShutdownHook Runtime.addShutdownHook}.
 32  *
 33  * @see java.lang.Runtime#addShutdownHook
 34  * @see java.lang.Runtime#removeShutdownHook
 35  */
 36 
 37 class ApplicationShutdownHooks {
 38     /* The set of registered hooks */
 39     private static IdentityHashMap<Thread, Thread> hooks;
 40     static {
 41         try {
 42             Shutdown.add(1 /* shutdown hook invocation order */,
 43                 false /* not registered if shutdown in progress */,
 44                 new Runnable() {
 45                     public void run() {
 46                         runHooks();
 47                     }

 80     static synchronized boolean remove(Thread hook) {
 81         if(hooks == null)
 82             throw new IllegalStateException("Shutdown in progress");
 83 
 84         if (hook == null)
 85             throw new NullPointerException();
 86 
 87         return hooks.remove(hook) != null;
 88     }
 89 
 90     /* Iterates over all application hooks creating a new thread for each
 91      * to run in. Hooks are run concurrently and this method waits for
 92      * them to finish.
 93      */
 94     static void runHooks() {
 95         Collection<Thread> threads;
 96         synchronized(ApplicationShutdownHooks.class) {
 97             threads = hooks.keySet();
 98             hooks = null;
 99         }
100 
101         for (Thread hook : threads) {
102             hook.start();







103         }
104         for (Thread hook : threads) {
105             while (true) {
106                 try {
107                     hook.join();
108                     break;
109                 } catch (InterruptedException ignored) {
110                 }
111             }
112         }
113     }
114 }

  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
 23  * questions.
 24  */
 25 package java.lang;
 26 
 27 import java.util.*;
 28 import java.util.concurrent.RejectedExecutionException;
 29 
 30 /*
 31  * Class to track and run user level shutdown hooks registered through
 32  * {@link Runtime#addShutdownHook Runtime.addShutdownHook}.
 33  *
 34  * @see java.lang.Runtime#addShutdownHook
 35  * @see java.lang.Runtime#removeShutdownHook
 36  */
 37 
 38 class ApplicationShutdownHooks {
 39     /* The set of registered hooks */
 40     private static IdentityHashMap<Thread, Thread> hooks;
 41     static {
 42         try {
 43             Shutdown.add(1 /* shutdown hook invocation order */,
 44                 false /* not registered if shutdown in progress */,
 45                 new Runnable() {
 46                     public void run() {
 47                         runHooks();
 48                     }

 81     static synchronized boolean remove(Thread hook) {
 82         if(hooks == null)
 83             throw new IllegalStateException("Shutdown in progress");
 84 
 85         if (hook == null)
 86             throw new NullPointerException();
 87 
 88         return hooks.remove(hook) != null;
 89     }
 90 
 91     /* Iterates over all application hooks creating a new thread for each
 92      * to run in. Hooks are run concurrently and this method waits for
 93      * them to finish.
 94      */
 95     static void runHooks() {
 96         Collection<Thread> threads;
 97         synchronized(ApplicationShutdownHooks.class) {
 98             threads = hooks.keySet();
 99             hooks = null;
100         }

101         for (Thread hook : threads) {
102             try {
103                 hook.start();
104             } catch (IllegalThreadStateException ignore) {
105                 // already started
106             } catch (RejectedExecutionException ignore) {
107                 // scheduler shutdown?
108                 assert hook.isVirtual();
109             }
110         }
111         for (Thread hook : threads) {
112             while (true) {
113                 try {
114                     hook.join();
115                     break;
116                 } catch (InterruptedException ignored) {
117                 }
118             }
119         }
120     }
121 }
< prev index next >