| 1 7 8 package java.lang; 9 10 import java.security.AccessController ; 11 import java.security.AccessControlContext ; 12 import java.security.PrivilegedAction ; 13 import java.util.Map ; 14 import java.util.HashMap ; 15 import java.util.Collections ; 16 import java.util.concurrent.locks.LockSupport ; 17 import sun.misc.SoftCache; 18 import sun.nio.ch.Interruptible; 19 import sun.security.util.SecurityConstants; 20 21 22 114 public 115 class Thread implements Runnable { 116 117 private static native void registerNatives(); 118 static { 119 registerNatives(); 120 } 121 122 private char name[]; 123 private int priority; 124 private Thread threadQ; 125 private long eetop; 126 private boolean started; 128 129 private boolean single_step; 130 131 132 private boolean daemon = false; 133 134 135 private boolean stillborn = false; 136 137 138 private Runnable target; 139 140 141 private ThreadGroup group; 142 143 144 private ClassLoader contextClassLoader; 145 146 147 private AccessControlContext inheritedAccessControlContext; 148 149 150 private static int threadInitNumber; 151 private static synchronized int nextThreadNum() { 152 return threadInitNumber++; 153 } 154 155 157 ThreadLocal.ThreadLocalMap threadLocals = null; 158 159 163 ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; 164 165 170 private long stackSize; 171 172 175 private long tid; 176 177 178 private static long threadSeqNumber; 179 180 183 private int threadStatus = 0; 184 185 186 private static synchronized long nextThreadID() { 187 return ++threadSeqNumber; 188 } 189 190 194 private volatile Interruptible blocker; 195 private Object blockerLock = new Object (); 196 197 199 void blockedOn(Interruptible b) { 200 synchronized (blockerLock) { 201 blocker = b; 202 } 203 } 204 205 208 public final static int MIN_PRIORITY = 1; 209 210 213 public final static int NORM_PRIORITY = 5; 214 215 218 public final static int MAX_PRIORITY = 10; 219 220 225 public static native Thread currentThread(); 226 227 231 public static native void yield(); 232 233 244 public static native void sleep(long millis) throws InterruptedException ; 245 246 261 public static void sleep(long millis, int nanos) 262 throws InterruptedException { 263 if (millis < 0) { 264 throw new IllegalArgumentException ("timeout value is negative"); 265 } 266 267 if (nanos < 0 || nanos > 999999) { 268 throw new IllegalArgumentException ( 269 "nanosecond timeout value out of range"); 270 } 271 272 if (nanos >= 500000 || (nanos != 0 && millis == 0)) { 273 millis++; 274 } 275 276 sleep(millis); 277 } 278 279 288 private void init(ThreadGroup g, Runnable target, String name, 289 long stackSize) { 290 Thread parent = currentThread(); 291 SecurityManager security = System.getSecurityManager(); 292 if (g == null) { 293 294 295 297 if (security != null) { 298 g = security.getThreadGroup(); 299 } 300 301 303 if (g == null) { 304 g = parent.getThreadGroup(); 305 } 306 } 307 308 310 g.checkAccess(); 311 312 315 if (security != null) { 316 if (isCCLOverridden(getClass())) { 317 security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION); 318 } 319 } 320 321 322 g.addUnstarted(); 323 324 this.group = g; 325 this.daemon = parent.isDaemon(); 326 this.priority = parent.getPriority(); 327 this.name = name.toCharArray(); 328 if (security == null || isCCLOverridden(parent.getClass())) 329 this.contextClassLoader = parent.getContextClassLoader(); 330 else 331 this.contextClassLoader = parent.contextClassLoader; 332 this.inheritedAccessControlContext = AccessController.getContext(); 333 this.target = target; 334 setPriority(priority); 335 if (parent.inheritableThreadLocals != null) 336 this.inheritableThreadLocals = 337 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); 338 339 this.stackSize = stackSize; 340 341 342 tid = nextThreadID(); 343 } 344 345 355 public Thread() { 356 init(null, null, "Thread-" + nextThreadNum(), 0); 357 } 358 359 370 public Thread(Runnable target) { 371 init(null, target, "Thread-" + nextThreadNum(), 0); 372 } 373 374 388 public Thread(ThreadGroup group, Runnable target) { 389 init(group, target, "Thread-" + nextThreadNum(), 0); 390 } 391 392 400 public Thread(String name) { 401 init(null, null, name, 0); 402 } 403 404 415 public Thread(ThreadGroup group, String name) { 416 init(group, null, name, 0); 417 } 418 419 428 public Thread(Runnable target, String name) { 429 init(null, target, name, 0); 430 } 431 432 486 public Thread(ThreadGroup group, Runnable target, String name) { 487 init(group, target, name, 0); 488 } 489 490 546 public Thread(ThreadGroup group, Runnable target, String name, 547 long stackSize) { 548 init(group, target, name, stackSize); 549 } 550 551 569 public synchronized void start() { 570 if (started) 571 throw new IllegalThreadStateException (); 572 started = true; 573 group.add(this); 574 start0(); 575 } 576 577 private native void start0(); 578 579 593 public void run() { 594 if (target != null) { 595 target.run(); 596 } 597 } 598 599 603 private void exit() { 604 if (group != null) { 605 group.remove(this); 606 group = null; 607 } 608 609 target = null; 610 611 threadLocals = null; 612 inheritableThreadLocals = null; 613 inheritedAccessControlContext = null; 614 blocker = null; 615 uncaughtExceptionHandler = null; 616 } 617 618 685 @Deprecated  686 public final void stop() { 687 synchronized (this) { 688 if (!this.isAlive()) return; 690 SecurityManager security = System.getSecurityManager(); 691 if (security != null) { 692 checkAccess(); 693 if (this != Thread.currentThread()) { 694 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION); 695 } 696 } 697 resume(); stop0(new ThreadDeath ()); 699 } 700 } 701 702 750 @Deprecated  751 public final synchronized void stop(Throwable obj) { 752 SecurityManager security = System.getSecurityManager(); 753 if (security != null) { 754 checkAccess(); 755 if ((this != Thread.currentThread()) || 756 (!(obj instanceof ThreadDeath ))) { 757 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION); 758 } 759 } 760 resume(); stop0(obj); 762 } 763 764 801 public void interrupt() { 802 if (this != Thread.currentThread()) 803 checkAccess(); 804 805 synchronized (blockerLock) { 806 Interruptible b = blocker; 807 if (b != null) { 808 interrupt0(); b.interrupt(); 810 return; 811 } 812 } 813 interrupt0(); 814 } 815 816 828 public static boolean interrupted() { 829 return currentThread().isInterrupted(true); 830 } 831 832 840 public boolean isInterrupted() { 841 return isInterrupted(false); 842 } 843 844 849 private native boolean isInterrupted(boolean ClearInterrupted); 850 851 868 @Deprecated  869 public void destroy() { 870 throw new NoSuchMethodError (); 871 } 872 873 880 public final native boolean isAlive(); 881 882 906 @Deprecated  907 public final void suspend() { 908 checkAccess(); 909 suspend0(); 910 } 911 912 932 @Deprecated  933 public final void resume() { 934 checkAccess(); 935 resume0(); 936 } 937 938 963 public final void setPriority(int newPriority) { 964 checkAccess(); 965 if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { 966 throw new IllegalArgumentException (); 967 } 968 if (newPriority > group.getMaxPriority()) { 969 newPriority = group.getMaxPriority(); 970 } 971 setPriority0(priority = newPriority); 972 } 973 974 981 public final int getPriority() { 982 return priority; 983 } 984 985 1000 public |