author | smarks |
Mon, 20 Dec 2010 13:47:04 -0800 | |
changeset 7803 | 56bc97d69d93 |
parent 5506 | 202f599c92aa |
child 9035 | 1255eb81cc2f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
* <tt>{@link Runtime#addShutdownHook Runtime.addShutdownHook}</tt>. |
|
32 |
* |
|
33 |
* @see java.lang.Runtime#addShutdownHook |
|
34 |
* @see java.lang.Runtime#removeShutdownHook |
|
35 |
*/ |
|
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 | 57 |
|
58 |
||
59 |
private ApplicationShutdownHooks() {} |
|
60 |
||
61 |
/* Add a new shutdown hook. Checks the shutdown state and the hook itself, |
|
62 |
* but does not do any security checks. |
|
63 |
*/ |
|
64 |
static synchronized void add(Thread hook) { |
|
65 |
if(hooks == null) |
|
66 |
throw new IllegalStateException("Shutdown in progress"); |
|
67 |
||
68 |
if (hook.isAlive()) |
|
69 |
throw new IllegalArgumentException("Hook already running"); |
|
70 |
||
71 |
if (hooks.containsKey(hook)) |
|
72 |
throw new IllegalArgumentException("Hook previously registered"); |
|
73 |
||
74 |
hooks.put(hook, hook); |
|
75 |
} |
|
76 |
||
77 |
/* Remove a previously-registered hook. Like the add method, this method |
|
78 |
* does not do any security checks. |
|
79 |
*/ |
|
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 |
*/ |
|
2277
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
94 |
static void runHooks() { |
2 | 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 |
try { |
|
106 |
hook.join(); |
|
107 |
} catch (InterruptedException x) { } |
|
108 |
} |
|
109 |
} |
|
110 |
} |