src/java.base/share/classes/java/lang/ApplicationShutdownHooks.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 39148 jdk/src/java.base/share/classes/java/lang/ApplicationShutdownHooks.java@7d619f151e36
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7803
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Class to track and run user level shutdown hooks registered through
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
    31
 * {@link Runtime#addShutdownHook Runtime.addShutdownHook}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @see java.lang.Runtime#addShutdownHook
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @see java.lang.Runtime#removeShutdownHook
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
2277
445a331b4a8b 6810254: Lazily instantiate the shared secret access objects
mchung
parents: 2
diff changeset
    37
class ApplicationShutdownHooks {
2703
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    38
    /* The set of registered hooks */
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    39
    private static IdentityHashMap<Thread, Thread> hooks;
2277
445a331b4a8b 6810254: Lazily instantiate the shared secret access objects
mchung
parents: 2
diff changeset
    40
    static {
2703
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    41
        try {
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    42
            Shutdown.add(1 /* shutdown hook invocation order */,
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    43
                false /* not registered if shutdown in progress */,
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    44
                new Runnable() {
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    45
                    public void run() {
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    46
                        runHooks();
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    47
                    }
2277
445a331b4a8b 6810254: Lazily instantiate the shared secret access objects
mchung
parents: 2
diff changeset
    48
                }
2703
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    49
            );
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    50
            hooks = new IdentityHashMap<>();
2703
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    51
        } catch (IllegalStateException e) {
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    52
            // application shutdown hooks cannot be added if
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    53
            // shutdown is in progress.
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    54
            hooks = null;
acd4d6a53e3e 6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents: 2277
diff changeset
    55
        }
2277
445a331b4a8b 6810254: Lazily instantiate the shared secret access objects
mchung
parents: 2
diff changeset
    56
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private ApplicationShutdownHooks() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /* Add a new shutdown hook.  Checks the shutdown state and the hook itself,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * but does not do any security checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static synchronized void add(Thread hook) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if(hooks == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new IllegalStateException("Shutdown in progress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (hook.isAlive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new IllegalArgumentException("Hook already running");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (hooks.containsKey(hook))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new IllegalArgumentException("Hook previously registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        hooks.put(hook, hook);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* Remove a previously-registered hook.  Like the add method, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * does not do any security checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static synchronized boolean remove(Thread hook) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if(hooks == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new IllegalStateException("Shutdown in progress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (hook == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return hooks.remove(hook) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* Iterates over all application hooks creating a new thread for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * to run in. Hooks are run concurrently and this method waits for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * them to finish.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
2277
445a331b4a8b 6810254: Lazily instantiate the shared secret access objects
mchung
parents: 2
diff changeset
    94
    static void runHooks() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Collection<Thread> threads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        synchronized(ApplicationShutdownHooks.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            threads = hooks.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            hooks = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        for (Thread hook : threads) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            hook.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (Thread hook : threads) {
39148
7d619f151e36 8154017: Shutdown hooks are racing against shutdown sequence, if System.exit()-calling thread is interrupted
chegar
parents: 32033
diff changeset
   105
            while (true) {
7d619f151e36 8154017: Shutdown hooks are racing against shutdown sequence, if System.exit()-calling thread is interrupted
chegar
parents: 32033
diff changeset
   106
                try {
7d619f151e36 8154017: Shutdown hooks are racing against shutdown sequence, if System.exit()-calling thread is interrupted
chegar
parents: 32033
diff changeset
   107
                    hook.join();
7d619f151e36 8154017: Shutdown hooks are racing against shutdown sequence, if System.exit()-calling thread is interrupted
chegar
parents: 32033
diff changeset
   108
                    break;
7d619f151e36 8154017: Shutdown hooks are racing against shutdown sequence, if System.exit()-calling thread is interrupted
chegar
parents: 32033
diff changeset
   109
                } catch (InterruptedException ignored) {
7d619f151e36 8154017: Shutdown hooks are racing against shutdown sequence, if System.exit()-calling thread is interrupted
chegar
parents: 32033
diff changeset
   110
                }
7d619f151e36 8154017: Shutdown hooks are racing against shutdown sequence, if System.exit()-calling thread is interrupted
chegar
parents: 32033
diff changeset
   111
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}