Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.
1 7 8 package java.lang; 9 10 import java.util.HashSet ; 11 import java.util.Iterator ; 12 13 14 22 23 class Shutdown { 24 25 28 private static class WrappedHook { 29 30 private Thread hook; 31 32 WrappedHook(Thread t) { 33 hook = t; 34 } 35 36 public int hashCode() { 37 return System.identityHashCode(hook); 38 } 39 40 public boolean equals(Object o) { 41 if (!(o instanceof WrappedHook)) return false; 42 return (((WrappedHook)o).hook == hook); 43 } 44 45 } 46 47 48 49 private static final int RUNNING = 0; 50 private static final int HOOKS = 1; 51 private static final int FINALIZERS = 2; 52 private static int state = RUNNING; 53 54 55 private static boolean runFinalizersOnExit = false; 56 57 58 private static HashSet hooks = null; 59 60 61 private static class Lock { }; 62 private static Object lock = new Lock(); 63 64 65 private static Object haltLock = new Lock(); 66 67 68 static void setRunFinalizersOnExit(boolean run) { 69 synchronized (lock) { 70 runFinalizersOnExit = run; 71 } 72 } 73 74 75 78 static void add(Thread hook) { 79 synchronized (lock) { 80 if (state > RUNNING) 81 throw new IllegalStateException ("Shutdown in progress"); 82 if (hook.isAlive()) 83 throw new IllegalArgumentException ("Hook already running"); 84 if (hooks == null) { 85 hooks = new HashSet (11); 86 hooks.add(new WrappedHook(hook)); 87 Terminator.setup(); 88 } else { 89 WrappedHook wh = new WrappedHook(hook); 90 if (hooks.contains(wh)) 91 throw new IllegalArgumentException ("Hook previously registered"); 92 hooks.add(wh); 93 } 94 } 95 } 96 97 98 101 static boolean remove(Thread hook) { 102 synchronized (lock) { 103 if (state > RUNNING) 104 throw new IllegalStateException ("Shutdown in progress"); 105 if (hook == null) throw new NullPointerException (); 106 if (hooks == null) { 107 return false; 108 } else { 109 boolean rv = hooks.remove(new WrappedHook(hook)); 110 if (rv && hooks.isEmpty()) { 111 hooks = null; 112 Terminator.teardown(); 113 } 114 return rv; 115 } 116 } 117 } 118 119 120 122 private static void runHooks() { 123 126 if (hooks == null) return; 127 for (Iterator i = hooks.iterator(); i.hasNext();) { 128 ((WrappedHook)(i.next())).hook.start(); 129 } 130 for (Iterator i = hooks.iterator(); i.hasNext();) { 131 try { 132 ((WrappedHook)(i.next())).hook.join(); 133 } catch (InterruptedException x) { 134 continue; 135 } 136 } 137 } 138 139 143 static void halt(int status) { 144 synchronized (haltLock) { 145 halt0(status); 146 } 147 } 148 149 static native void halt0(int status); 150 151 152 private static native void runAllFinalizers(); 153 154 155 166 private static void sequence() { 167 synchronized (lock) { 168 171 if (state != HOOKS) return; 172 } 173 runHooks(); 174 boolean rfoe; 175 synchronized (lock) { 176 state = FINALIZERS; 177 rfoe = runFinalizersOnExit; 178 } 179 if (rfoe) runAllFinalizers(); 180 } 181 182 183 187 static void exit(int status) { 188 boolean runMoreFinalizers = false; 189 synchronized (lock) { 190 if (status != 0) runFinalizersOnExit = false; 191 switch (state) { 192 case RUNNING: 193 state = HOOKS; 194 break; 195 case HOOKS: 196 break; 197 case FINALIZERS: 198 if (status != 0) { 199 200 halt(status); 201 } else { 202 205 runMoreFinalizers = runFinalizersOnExit; 206 } 207 break; 208 } 209 } 210 if (runMoreFinalizers) { 211 runAllFinalizers(); 212 halt(status); 213 } 214 synchronized (Shutdown .class) { 215 218 sequence(); 219 halt(status); 220 } 221 } 222 223 224 228 static void shutdown() { 229 synchronized (lock) { 230 switch (state) { 231 case RUNNING: 232 state = HOOKS; 233 break; 234 case HOOKS: 235 case FINALIZERS: 236 break; 237 } 238 } 239 synchronized (Shutdown .class) { 240 sequence(); 241 } 242 } 243 244 } 245
| Popular Tags
|