1 /* 2 * Copyright (c) 1994, 2009, Oracle and/or its affiliates. All rights reserved. 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 7 * published by the Free Software Foundation. Oracle designates this 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 26 package java.lang; 27 28 import java.lang.ref.Reference; 29 import java.lang.ref.ReferenceQueue; 30 import java.lang.ref.WeakReference; 31 import java.security.AccessController; 32 import java.security.AccessControlContext; 33 import java.security.PrivilegedAction; 34 import java.util.Map; 35 import java.util.HashMap; 36 import java.util.concurrent.ConcurrentHashMap; 37 import java.util.concurrent.ConcurrentMap; 38 import java.util.concurrent.locks.LockSupport; 39 import sun.nio.ch.Interruptible; 40 import sun.security.util.SecurityConstants; 41 42 43 /** 44 * A <i>thread</i> is a thread of execution in a program. The Java 45 * Virtual Machine allows an application to have multiple threads of 46 * execution running concurrently. 47 * <p> 48 * Every thread has a priority. Threads with higher priority are 49 * executed in preference to threads with lower priority. Each thread 50 * may or may not also be marked as a daemon. When code running in 51 * some thread creates a new <code>Thread</code> object, the new 52 * thread has its priority initially set equal to the priority of the 53 * creating thread, and is a daemon thread if and only if the 54 * creating thread is a daemon. 55 * <p> 56 * When a Java Virtual Machine starts up, there is usually a single 57 * non-daemon thread (which typically calls the method named 58 * <code>main</code> of some designated class). The Java Virtual 59 * Machine continues to execute threads until either of the following 60 * occurs: 61 * <ul> 62 * <li>The <code>exit</code> method of class <code>Runtime</code> has been 63 * called and the security manager has permitted the exit operation 64 * to take place. 65 * <li>All threads that are not daemon threads have died, either by 66 * returning from the call to the <code>run</code> method or by 67 * throwing an exception that propagates beyond the <code>run</code> 68 * method. 69 * </ul> 70 * <p> 71 * There are two ways to create a new thread of execution. One is to 72 * declare a class to be a subclass of <code>Thread</code>. This 73 * subclass should override the <code>run</code> method of class 74 * <code>Thread</code>. An instance of the subclass can then be 75 * allocated and started. For example, a thread that computes primes 76 * larger than a stated value could be written as follows: 77 * <p><hr><blockquote><pre> 78 * class PrimeThread extends Thread { 79 * long minPrime; 80 * PrimeThread(long minPrime) { 81 * this.minPrime = minPrime; 82 * } 83 * 84 * public void run() { 85 * // compute primes larger than minPrime 86 * . . . 87 * } 88 * } 89 * </pre></blockquote><hr> 90 * <p> 91 * The following code would then create a thread and start it running: 92 * <p><blockquote><pre> 93 * PrimeThread p = new PrimeThread(143); 94 * p.start(); 95 * </pre></blockquote> 96 * <p> 97 * The other way to create a thread is to declare a class that 98 * implements the <code>Runnable</code> interface. That class then 99 * implements the <code>run</code> method. An instance of the class can 100 * then be allocated, passed as an argument when creating 101 * <code>Thread</code>, and started. The same example in this other 102 * style looks like the following: 103 * <p><hr><blockquote><pre> 104 * class PrimeRun implements Runnable { 105 * long minPrime; 106 * PrimeRun(long minPrime) { 107 * this.minPrime = minPrime; 108 * } 109 * 110 * public void run() { 111 * // compute primes larger than minPrime 112 * . . . 113 * } 114 * } 115 * </pre></blockquote><hr> 116 * <p> 117 * The following code would then create a thread and start it running: 118 * <p><blockquote><pre> 119 * PrimeRun p = new PrimeRun(143); 120 * new Thread(p).start(); 121 * </pre></blockquote> 122 * <p> 123 * Every thread has a name for identification purposes. More than 124 * one thread may have the same name. If a name is not specified when 125 * a thread is created, a new name is generated for it. 126 * <p> 127 * Unless otherwise noted, passing a {@code null} argument to a constructor 128 * or method in this class will cause a {@link NullPointerException} to be 129 * thrown. 130 * 131 * @author unascribed 132 * @see Runnable 133 * @see Runtime#exit(int) 134 * @see #run() 135 * @see #stop() 136 * @since JDK1.0 137 */ 138 public 139 class Thread implements Runnable { 140 /* Make sure registerNatives is the first thing <clinit> does. */ 141 private static native void registerNatives(); 142 static { 143 registerNatives(); 144 } 145 146 private char name[]; 147 private int priority; 148 private Thread threadQ; 149 private long eetop; 150 151 /* Whether or not to single_step this thread. */ 152 private boolean single_step; 153 154 /* Whether or not the thread is a daemon thread. */ 155 private boolean daemon = false; 156 157 /* JVM state */ 158 private boolean stillborn = false; 159 160 /* What will be run. */ 161 private Runnable target; 162 163 /* The group of this thread */ 164 private ThreadGroup group; 165 166 /* The context ClassLoader for this thread */ 167 private ClassLoader contextClassLoader; 168 169 /* The inherited AccessControlContext of this thread */ 170 private AccessControlContext inheritedAccessControlContext; 171 172 /* For autonumbering anonymous threads. */ 173 private static int threadInitNumber; 174 private static synchronized int nextThreadNum() { 175 return threadInitNumber++; 176 } 177 178 /* ThreadLocal values pertaining to this thread. This map is maintained 179 * by the ThreadLocal class. */ 180 ThreadLocal.ThreadLocalMap threadLocals = null; 181 182 /* 183 * InheritableThreadLocal values pertaining to this thread. This map is 184 * maintained by the InheritableThreadLocal class. 185 */ 186 ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; 187 188 /* 189 * The requested stack size for this thread, or 0 if the creator did 190 * not specify a stack size. It is up to the VM to do whatever it 191 * likes with this number; some VMs will ignore it. 192 */ 193 private long stackSize; 194 195 /* 196 * JVM-private state that persists after native thread termination. 197 */ 198 private long nativeParkEventPointer; 199 200 /* 201 * Thread ID 202 */ 203 private long tid; 204 205 /* For generating thread ID */ 206 private static long threadSeqNumber; 207 208 /* Java thread status for tools, 209 * initialized to indicate thread 'not yet started' 210 */ 211 212 private volatile int threadStatus = 0; 213 214 215 private static synchronized long nextThreadID() { 216 return ++threadSeqNumber; 217 } 218 219 /** 220 * The argument supplied to the current call to 221 * java.util.concurrent.locks.LockSupport.park. 222 * Set by (private) java.util.concurrent.locks.LockSupport.setBlocker 223 * Accessed using java.util.concurrent.locks.LockSupport.getBlocker 224 */ 225 volatile Object parkBlocker; 226 227 /* The object in which this thread is blocked in an interruptible I/O 228 * operation, if any. The blocker's interrupt method should be invoked 229 * after setting this thread's interrupt status. 230 */ 231 private volatile Interruptible blocker; 232 private final Object blockerLock = new Object(); 233 234 /* Set the blocker field; invoked via sun.misc.SharedSecrets from java.nio code 235 */ 236 void blockedOn(Interruptible b) { 237 synchronized (blockerLock) { 238 blocker = b; 239 } 240 } 241 242 /** 243 * The minimum priority that a thread can have. 244 */ 245 public final static int MIN_PRIORITY = 1; 246 247 /** 248 * The default priority that is assigned to a thread. 249 */ 250 public final static int NORM_PRIORITY = 5; 251 252 /** 253 * The maximum priority that a thread can have. 254 */ 255 public final static int MAX_PRIORITY = 10; 256 257 /* If stop was called before start */ 258 private boolean stopBeforeStart; 259 260 /* Remembered Throwable from stop before start */ 261 private Throwable throwableFromStop; 262 263 /** 264 * Returns a reference to the currently executing thread object. 265 * 266 * @return the currently executing thread. 267 */ 268 public static native Thread currentThread(); 269 270 /** 271 * A hint to the scheduler that the current thread is willing to yield 272 * its current use of a processor. The scheduler is free to ignore this 273 * hint. 274 * 275 * <p> Yield is a heuristic attempt to improve relative progression 276 * between threads that would otherwise over-utilise a CPU. Its use 277 * should be combined with detailed profiling and benchmarking to 278 * ensure that it actually has the desired effect. 279 * 280 * <p> It is rarely appropriate to use this method. It may be useful 281 * for debugging or testing purposes, where it may help to reproduce 282 * bugs due to race conditions. It may also be useful when designing 283 * concurrency control constructs such as the ones in the 284 * {@link java.util.concurrent.locks} package. 285 */ 286 public static native void yield(); 287 288 /** 289 * Causes the currently executing thread to sleep (temporarily cease 290 * execution) for the specified number of milliseconds, subject to 291 * the precision and accuracy of system timers and schedulers. The thread 292 * does not lose ownership of any monitors. 293 * 294 * @param millis 295 * the length of time to sleep in milliseconds 296 * 297 * @throws IllegalArgumentException 298 * if the value of {@code millis} is negative 299 * 300 * @throws InterruptedException 301 * if any thread has interrupted the current thread. The 302 * <i>interrupted status</i> of the current thread is 303 * cleared when this exception is thrown. 304 */ 305 public static native void sleep(long millis) throws InterruptedException; 306 307 /** 308 * Causes the currently executing thread to sleep (temporarily cease 309 * execution) for the specified number of milliseconds plus the specified 310 * number of nanoseconds, subject to the precision and accuracy of system 311 * timers and schedulers. The thread does not lose ownership of any 312 * monitors. 313 * 314 * @param millis 315 * the length of time to sleep in milliseconds 316 * 317 * @param nanos 318 * {@code 0-999999} additional nanoseconds to sleep 319 * 320 * @throws IllegalArgumentException 321 * if the value of {@code millis} is negative, or the value of 322 * {@code nanos} is not in the range {@code 0-999999} 323 * 324 * @throws InterruptedException 325 * if any thread has interrupted the current thread. The 326 * <i>interrupted status</i> of the current thread is 327 * cleared when this exception is thrown. 328 */ 329 public static void sleep(long millis, int nanos) 330 throws InterruptedException { 331 if (millis < 0) { 332 throw new IllegalArgumentException("timeout value is negative"); 333 } 334 335 if (nanos < 0 || nanos > 999999) { 336 throw new IllegalArgumentException( 337 "nanosecond timeout value out of range"); 338 } 339 340 if (nanos >= 500000 || (nanos != 0 && millis == 0)) { 341 millis++; 342 } 343 344 sleep(millis); 345 } 346 347 /** 348 * Initializes a Thread. 349 * 350 * @param g the Thread group 351 * @param target the object whose run() method gets called 352 * @param name the name of the new Thread 353 * @param stackSize the desired stack size for the new thread, or 354 * zero to indicate that this parameter is to be ignored. 355 */ 356 private void init(ThreadGroup g, Runnable target, String name, 357 long stackSize) { 358 if (name == null) { 359 throw new NullPointerException("name cannot be null"); 360 } 361 362 Thread parent = currentThread(); 363 SecurityManager security = System.getSecurityManager(); 364 if (g == null) { 365 /* Determine if it's an applet or not */ 366 367 /* If there is a security manager, ask the security manager 368 what to do. */ 369 if (security != null) { 370 g = security.getThreadGroup(); 371 } 372 373 /* If the security doesn't have a strong opinion of the matter 374 use the parent thread group. */ 375 if (g == null) { 376 g = parent.getThreadGroup(); 377 } 378 } 379 380 /* checkAccess regardless of whether or not threadgroup is 381 explicitly passed in. */ 382 g.checkAccess(); 383 384 /* 385 * Do we have the required permissions? 386 */ 387 if (security != null) { 388 if (isCCLOverridden(getClass())) { 389 security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION); 390 } 391 } 392 393 g.addUnstarted(); 394 395 this.group = g; 396 this.daemon = parent.isDaemon(); 397 this.priority = parent.getPriority(); 398 this.name = name.toCharArray(); 399 if (security == null || isCCLOverridden(parent.getClass())) 400 this.contextClassLoader = parent.getContextClassLoader(); 401 else 402 this.contextClassLoader = parent.contextClassLoader; 403 this.inheritedAccessControlContext = AccessController.getContext(); 404 this.target = target; 405 setPriority(priority); 406 if (parent.inheritableThreadLocals != null) 407 this.inheritableThreadLocals = 408 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); 409 /* Stash the specified stack size in case the VM cares */ 410 this.stackSize = stackSize; 411 412 /* Set thread ID */ 413 tid = nextThreadID(); 414 } 415 416 /** 417 * Throws CloneNotSupportedException as a Thread can not be meaningfully 418 * cloned. Construct a new Thread instead. 419 * 420 * @throws CloneNotSupportedException 421 * always 422 */ 423 @Override 424 protected Object clone() throws CloneNotSupportedException { 425 throw new CloneNotSupportedException(); 426 } 427 428 /** 429 * Allocates a new {@code Thread} object. This constructor has the same 430 * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 431 * {@code (null, null, gname)}, where {@code gname} is a newly generated 432 * name. Automatically generated names are of the form 433 * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer. 434 */ 435 public Thread() { 436 init(null, null, "Thread-" + nextThreadNum(), 0); 437 } 438 439 /** 440 * Allocates a new {@code Thread} object. This constructor has the same 441 * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 442 * {@code (null, target, gname)}, where {@code gname} is a newly generated 443 * name. Automatically generated names are of the form 444 * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer. 445 * 446 * @param target 447 * the object whose {@code run} method is invoked when this thread 448 * is started. If {@code null}, this classes {@code run} method does 449 * nothing. 450 */ 451 public Thread(Runnable target) { 452 init(null, target, "Thread-" + nextThreadNum(), 0); 453 } 454 455 /** 456 * Allocates a new {@code Thread} object. This constructor has the same 457 * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 458 * {@code (group, target, gname)} ,where {@code gname} is a newly generated 459 * name. Automatically generated names are of the form 460 * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer. 461 * 462 * @param group 463 * the thread group. If {@code null} and there is a security 464 * manager, the group is determined by {@linkplain 465 * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}. 466 * If there is not a security manager or {@code 467 * SecurityManager.getThreadGroup()} returns {@code null}, the group 468 * is set to the current thread's thread group. 469 * 470 * @param target 471 * the object whose {@code run} method is invoked when this thread 472 * is started. If {@code null}, this thread's run method is invoked. 473 * 474 * @throws SecurityException 475 * if the current thread cannot create a thread in the specified 476 * thread group 477 */ 478 public Thread(ThreadGroup group, Runnable target) { 479 init(group, target, "Thread-" + nextThreadNum(), 0); 480 } 481 482 /** 483 * Allocates a new {@code Thread} object. This constructor has the same 484 * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 485 * {@code (null, null, name)}. 486 * 487 * @param name 488 * the name of the new thread 489 */ 490 public Thread(String name) { 491 init(null, null, name, 0); 492 } 493 494 /** 495 * Allocates a new {@code Thread} object. This constructor has the same 496 * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 497 * {@code (group, null, name)}. 498 * 499 * @param group 500 * the thread group. If {@code null} and there is a security 501 * manager, the group is determined by {@linkplain 502 * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}. 503 * If there is not a security manager or {@code 504 * SecurityManager.getThreadGroup()} returns {@code null}, the group 505 * is set to the current thread's thread group. 506 * 507 * @param name 508 * the name of the new thread 509 * 510 * @throws SecurityException 511 * if the current thread cannot create a thread in the specified 512 * thread group 513 */ 514 public Thread(ThreadGroup group, String name) { 515 init(group, null, name, 0); 516 } 517 518 /** 519 * Allocates a new {@code Thread} object. This constructor has the same 520 * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 521 * {@code (null, target, name)}. 522 * 523 * @param target 524 * the object whose {@code run} method is invoked when this thread 525 * is started. If {@code null}, this thread's run method is invoked. 526 * 527 * @param name 528 * the name of the new thread 529 */ 530 public Thread(Runnable target, String name) { 531 init(null, target, name, 0); 532 } 533 534 /** 535 * Allocates a new {@code Thread} object so that it has {@code target} 536 * as its run object, has the specified {@code name} as its name, 537 * and belongs to the thread group referred to by {@code group}. 538 * 539 * <p>If there is a security manager, its 540 * {@link SecurityManager#checkAccess(ThreadGroup) checkAccess} 541 * method is invoked with the ThreadGroup as its argument. 542 * 543 * <p>In addition, its {@code checkPermission} method is invoked with 544 * the {@code RuntimePermission("enableContextClassLoaderOverride")} 545 * permission when invoked directly or indirectly by the constructor 546 * of a subclass which overrides the {@code getContextClassLoader} 547 * or {@code setContextClassLoader} methods. 548 * 549 * <p>The priority of the newly created thread is set equal to the 550 * priority of the thread creating it, that is, the currently running 551 * thread. The method {@linkplain #setPriority setPriority} may be 552 * used to change the priority to a new value. 553 * 554 * <p>The newly created thread is initially marked as being a daemon 555 * thread if and only if the thread creating it is currently marked 556 * as a daemon thread. The method {@linkplain #setDaemon setDaemon} 557 * may be used to change whether or not a thread is a daemon. 558 * 559 * @param group 560 * the thread group. If {@code null} and there is a security 561 * manager, the group is determined by {@linkplain 562 * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}. 563 * If there is not a security manager or {@code 564 * SecurityManager.getThreadGroup()} returns {@code null}, the group 565 * is set to the current thread's thread group. 566 * 567 * @param target 568 * the object whose {@code run} method is invoked when this thread 569 * is started. If {@code null}, this thread's run method is invoked. 570 * 571 * @param name 572 * the name of the new thread 573 * 574 * @throws SecurityException 575 * if the current thread cannot create a thread in the specified 576 * thread group or cannot override the context class loader methods. 577 */ 578 public Thread(ThreadGroup group, Runnable target, String name) { 579 init(group, target, name, 0); 580 } 581 582 /** 583 * Allocates a new {@code Thread} object so that it has {@code target} 584 * as its run object, has the specified {@code name} as its name, 585 * and belongs to the thread group referred to by {@code group}, and has 586 * the specified <i>stack size</i>. 587 * 588 * <p>This constructor is identical to {@link 589 * #Thread(ThreadGroup,Runnable,String)} with the exception of the fact 590 * that it allows the thread stack size to be specified. The stack size 591 * is the approximate number of bytes of address space that the virtual 592 * machine is to allocate for this thread's stack. <b>The effect of the 593 * {@code stackSize} parameter, if any, is highly platform dependent.</b> 594 * 595 * <p>On some platforms, specifying a higher value for the 596 * {@code stackSize} parameter may allow a thread to achieve greater 597 * recursion depth before throwing a {@link StackOverflowError}. 598 * Similarly, specifying a lower value may allow a greater number of 599 * threads to exist concurrently without throwing an {@link 600 * OutOfMemoryError} (or other internal error). The details of 601 * the relationship between the value of the <tt>stackSize</tt> parameter 602 * and the maximum recursion depth and concurrency level are 603 * platform-dependent. <b>On some platforms, the value of the 604 * {@code stackSize} parameter may have no effect whatsoever.</b> 605 * 606 * <p>The virtual machine is free to treat the {@code stackSize} 607 * parameter as a suggestion. If the specified value is unreasonably low 608 * for the platform, the virtual machine may instead use some 609 * platform-specific minimum value; if the specified value is unreasonably 610 * high, the virtual machine may instead use some platform-specific 611 * maximum. Likewise, the virtual machine is free to round the specified 612 * value up or down as it sees fit (or to ignore it completely). 613 * 614 * <p>Specifying a value of zero for the {@code stackSize} parameter will 615 * cause this constructor to behave exactly like the 616 * {@code Thread(ThreadGroup, Runnable, String)} constructor. 617 * 618 * <p><i>Due to the platform-dependent nature of the behavior of this 619 * constructor, extreme care should be exercised in its use. 620 * The thread stack size necessary to perform a given computation will 621 * likely vary from one JRE implementation to another. In light of this 622 * variation, careful tuning of the stack size parameter may be required, 623 * and the tuning may need to be repeated for each JRE implementation on 624 * which an application is to run.</i> 625 * 626 * <p>Implementation note: Java platform implementers are encouraged to 627 * document their implementation's behavior with respect to the 628 * {@code stackSize} parameter. 629 * 630 * 631 * @param group 632 * the thread group. If {@code null} and there is a security 633 * manager, the group is determined by {@linkplain 634 * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}. 635 * If there is not a security manager or {@code 636 * SecurityManager.getThreadGroup()} returns {@code null}, the group 637 * is set to the current thread's thread group. 638 * 639 * @param target 640 * the object whose {@code run} method is invoked when this thread 641 * is started. If {@code null}, this thread's run method is invoked. 642 * 643 * @param name 644 * the name of the new thread 645 * 646 * @param stackSize 647 * the desired stack size for the new thread, or zero to indicate 648 * that this parameter is to be ignored. 649 * 650 * @throws SecurityException 651 * if the current thread cannot create a thread in the specified 652 * thread group 653 * 654 * @since 1.4 655 */ 656 public Thread(ThreadGroup group, Runnable target, String name, 657 long stackSize) { 658 init(group, target, name, stackSize); 659 } 660 661 /** 662 * Causes this thread to begin execution; the Java Virtual Machine 663 * calls the <code>run</code> method of this thread. 664 * <p> 665 * The result is that two threads are running concurrently: the 666 * current thread (which returns from the call to the 667 * <code>start</code> method) and the other thread (which executes its 668 * <code>run</code> method). 669 * <p> 670 * It is never legal to start a thread more than once. 671 * In particular, a thread may not be restarted once it has completed 672 * execution. 673 * 674 * @exception IllegalThreadStateException if the thread was already 675 * started. 676 * @see #run() 677 * @see #stop() 678 */ 679 public synchronized void start() { 680 /** 681 * This method is not invoked for the main method thread or "system" 682 * group threads created/set up by the VM. Any new functionality added 683 * to this method in the future may have to also be added to the VM. 684 * 685 * A zero status value corresponds to state "NEW". 686 */ 687 if (threadStatus != 0) 688 throw new IllegalThreadStateException(); 689 690 /* Notify the group that this thread is about to be started 691 * so that it can be added to the group's list of threads 692 * and the group's unstarted count can be decremented. */ 693 group.threadStarting(this); 694 695 boolean started = false; 696 try { 697 start0(); 698 started = true; 699 } finally { 700 try { 701 if (!started) { 702 group.threadStartFailed(this); 703 } 704 } catch (Throwable ignore) { 705 /* do nothing. If start0 threw a Throwable then 706 it will be passed up the call stack */ 707 } 708 } 709 710 if (stopBeforeStart) { 711 stop0(throwableFromStop); 712 } 713 } 714 715 private native void start0(); 716 717 /** 718 * If this thread was constructed using a separate 719 * <code>Runnable</code> run object, then that 720 * <code>Runnable</code> object's <code>run</code> method is called; 721 * otherwise, this method does nothing and returns. 722 * <p> 723 * Subclasses of <code>Thread</code> should override this method. 724 * 725 * @see #start() 726 * @see #stop() 727 * @see #Thread(ThreadGroup, Runnable, String) 728 */ 729 @Override 730 public void run() { 731 if (target != null) { 732 target.run(); 733 } 734 } 735 736 /** 737 * This method is called by the system to give a Thread 738 * a chance to clean up before it actually exits. 739 */ 740 private void exit() { 741 if (group != null) { 742 group.threadTerminated(this); 743 group = null; 744 } 745 /* Aggressively null out all reference fields: see bug 4006245 */ 746 target = null; 747 /* Speed the release of some of these resources */ 748 threadLocals = null; 749 inheritableThreadLocals = null; 750 inheritedAccessControlContext = null; 751 blocker = null; 752 uncaughtExceptionHandler = null; 753 } 754 755 /** 756 * Forces the thread to stop executing. 757 * <p> 758 * If there is a security manager installed, its <code>checkAccess</code> 759 * method is called with <code>this</code> 760 * as its argument. This may result in a 761 * <code>SecurityException</code> being raised (in the current thread). 762 * <p> 763 * If this thread is different from the current thread (that is, the current 764 * thread is trying to stop a thread other than itself), the 765 * security manager's <code>checkPermission</code> method (with a 766 * <code>RuntimePermission("stopThread")</code> argument) is called in 767 * addition. 768 * Again, this may result in throwing a 769 * <code>SecurityException</code> (in the current thread). 770 * <p> 771 * The thread represented by this thread is forced to stop whatever 772 * it is doing abnormally and to throw a newly created 773 * <code>ThreadDeath</code> object as an exception. 774 * <p> 775 * It is permitted to stop a thread that has not yet been started. 776 * If the thread is eventually started, it immediately terminates. 777 * <p> 778 * An application should not normally try to catch 779 * <code>ThreadDeath</code> unless it must do some extraordinary 780 * cleanup operation (note that the throwing of 781 * <code>ThreadDeath</code> causes <code>finally</code> clauses of 782 * <code>try</code> statements to be executed before the thread 783 * officially dies). If a <code>catch</code> clause catches a 784 * <code>ThreadDeath</code> object, it is important to rethrow the 785 * object so that the thread actually dies. 786 * <p> 787 * The top-level error handler that reacts to otherwise uncaught 788 * exceptions does not print out a message or otherwise notify the 789 * application if the uncaught exception is an instance of 790 * <code>ThreadDeath</code>. 791 * 792 * @exception SecurityException if the current thread cannot 793 * modify this thread. 794 * @see #interrupt() 795 * @see #checkAccess() 796 * @see #run() 797 * @see #start() 798 * @see ThreadDeath 799 * @see ThreadGroup#uncaughtException(Thread,Throwable) 800 * @see SecurityManager#checkAccess(Thread) 801 * @see SecurityManager#checkPermission 802 * @deprecated This method is inherently unsafe. Stopping a thread with 803 * Thread.stop causes it to unlock all of the monitors that it 804 * has locked (as a natural consequence of the unchecked 805 * <code>ThreadDeath</code> exception propagating up the stack). If 806 * any of the objects previously protected by these monitors were in 807 * an inconsistent state, the damaged objects become visible to 808 * other threads, potentially resulting in arbitrary behavior. Many 809 * uses of <code>stop</code> should be replaced by code that simply 810 * modifies some variable to indicate that the target thread should 811 * stop running. The target thread should check this variable 812 * regularly, and return from its run method in an orderly fashion 813 * if the variable indicates that it is to stop running. If the 814 * target thread waits for long periods (on a condition variable, 815 * for example), the <code>interrupt</code> method should be used to 816 * interrupt the wait. 817 * For more information, see 818 * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why 819 * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 820 */ 821 @Deprecated 822 public final void stop() { 823 // If the thread is already dead, return. 824 // A zero status value corresponds to "NEW". 825 if ((threadStatus != 0) && !isAlive()) { 826 return; 827 } 828 stop1(new ThreadDeath()); 829 } 830 831 /** 832 * Forces the thread to stop executing. 833 * <p> 834 * If there is a security manager installed, the <code>checkAccess</code> 835 * method of this thread is called, which may result in a 836 * <code>SecurityException</code> being raised (in the current thread). 837 * <p> 838 * If this thread is different from the current thread (that is, the current 839 * thread is trying to stop a thread other than itself) or 840 * <code>obj</code> is not an instance of <code>ThreadDeath</code>, the 841 * security manager's <code>checkPermission</code> method (with the 842 * <code>RuntimePermission("stopThread")</code> argument) is called in 843 * addition. 844 * Again, this may result in throwing a 845 * <code>SecurityException</code> (in the current thread). 846 * <p> 847 * If the argument <code>obj</code> is null, a 848 * <code>NullPointerException</code> is thrown (in the current thread). 849 * <p> 850 * The thread represented by this thread is forced to stop 851 * whatever it is doing abnormally and to throw the 852 * <code>Throwable</code> object <code>obj</code> as an exception. This 853 * is an unusual action to take; normally, the <code>stop</code> method 854 * that takes no arguments should be used. 855 * <p> 856 * It is permitted to stop a thread that has not yet been started. 857 * If the thread is eventually started, it immediately terminates. 858 * 859 * @param obj the Throwable object to be thrown. 860 * @exception SecurityException if the current thread cannot modify 861 * this thread. 862 * @throws NullPointerException if obj is <tt>null</tt>. 863 * @see #interrupt() 864 * @see #checkAccess() 865 * @see #run() 866 * @see #start() 867 * @see #stop() 868 * @see SecurityManager#checkAccess(Thread) 869 * @see SecurityManager#checkPermission 870 * @deprecated This method is inherently unsafe. See {@link #stop()} 871 * for details. An additional danger of this 872 * method is that it may be used to generate exceptions that the 873 * target thread is unprepared to handle (including checked 874 * exceptions that the thread could not possibly throw, were it 875 * not for this method). 876 * For more information, see 877 * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why 878 * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 879 */ 880 @Deprecated 881 public final synchronized void stop(Throwable obj) { 882 stop1(obj); 883 } 884 885 /** 886 * Common impl for stop() and stop(Throwable). 887 */ 888 private final synchronized void stop1(Throwable th) { 889 SecurityManager security = System.getSecurityManager(); 890 if (security != null) { 891 checkAccess(); 892 if ((this != Thread.currentThread()) || 893 (!(th instanceof ThreadDeath))) { 894 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION); 895 } 896 } 897 // A zero status value corresponds to "NEW" 898 if (threadStatus != 0) { 899 resume(); // Wake up thread if it was suspended; no-op otherwise 900 stop0(th); 901 } else { 902 903 // Must do the null arg check that the VM would do with stop0 904 if (th == null) { 905 throw new NullPointerException(); 906 } 907 908 // Remember this stop attempt for if/when start is used 909 stopBeforeStart = true; 910 throwableFromStop = th; 911 } 912 } 913 914 /** 915 * Interrupts this thread. 916 * 917 * <p> Unless the current thread is interrupting itself, which is 918 * always permitted, the {@link #checkAccess() checkAccess} method 919 * of this thread is invoked, which may cause a {@link 920 * SecurityException} to be thrown. 921 * 922 * <p> If this thread is blocked in an invocation of the {@link 923 * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link 924 * Object#wait(long, int) wait(long, int)} methods of the {@link Object} 925 * class, or of the {@link #join()}, {@link #join(long)}, {@link 926 * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)}, 927 * methods of this class, then its interrupt status will be cleared and it 928 * will receive an {@link InterruptedException}. 929 * 930 * <p> If this thread is blocked in an I/O operation upon an {@link 931 * java.nio.channels.InterruptibleChannel </code>interruptible 932 * channel<code>} then the channel will be closed, the thread's interrupt 933 * status will be set, and the thread will receive a {@link 934 * java.nio.channels.ClosedByInterruptException}. 935 * 936 * <p> If this thread is blocked in a {@link java.nio.channels.Selector} 937 * then the thread's interrupt status will be set and it will return 938 * immediately from the selection operation, possibly with a non-zero 939 * value, just as if the selector's {@link 940 * java.nio.channels.Selector#wakeup wakeup} method were invoked. 941 * 942 * <p> If none of the previous conditions hold then this thread's interrupt 943 * status will be set. </p> 944 * 945 * <p> Interrupting a thread that is not alive need not have any effect. 946 * 947 * @throws SecurityException 948 * if the current thread cannot modify this thread 949 * 950 * @revised 6.0 951 * @spec JSR-51 952 */ 953 public void interrupt() { 954 if (this != Thread.currentThread()) 955 checkAccess(); 956 957 synchronized (blockerLock) { 958 Interruptible b = blocker; 959 if (b != null) { 960 interrupt0(); // Just to set the interrupt flag 961 b.interrupt(this); 962 return; 963 } 964 } 965 interrupt0(); 966 } 967 968 /** 969 * Tests whether the current thread has been interrupted. The 970 * <i>interrupted status</i> of the thread is cleared by this method. In 971 * other words, if this method were to be called twice in succession, the 972 * second call would return false (unless the current thread were 973 * interrupted again, after the first call had cleared its interrupted 974 * status and before the second call had examined it). 975 * 976 * <p>A thread interruption ignored because a thread was not alive 977 * at the time of the interrupt will be reflected by this method 978 * returning false. 979 * 980 * @return <code>true</code> if the current thread has been interrupted; 981 * <code>false</code> otherwise. 982 * @see #isInterrupted() 983 * @revised 6.0 984 */ 985 public static boolean interrupted() { 986 return currentThread().isInterrupted(true); 987 } 988 989 /** 990 * Tests whether this thread has been interrupted. The <i>interrupted 991 * status</i> of the thread is unaffected by this method. 992 * 993 * <p>A thread interruption ignored because a thread was not alive 994 * at the time of the interrupt will be reflected by this method 995 * returning false. 996 * 997 * @return <code>true</code> if this thread has been interrupted; 998 * <code>false</code> otherwise. 999 * @see #interrupted() 1000 * @revised 6.0 1001 */ 1002 public boolean isInterrupted() { 1003 return isInterrupted(false); 1004 } 1005 1006 /** 1007 * Tests if some Thread has been interrupted. The interrupted state 1008 * is reset or not based on the value of ClearInterrupted that is 1009 * passed. 1010 */ 1011 private native boolean isInterrupted(boolean ClearInterrupted); 1012 1013 /** 1014 * Throws {@link NoSuchMethodError}. 1015 * 1016 * @deprecated This method was originally designed to destroy this 1017 * thread without any cleanup. Any monitors it held would have 1018 * remained locked. However, the method was never implemented. 1019 * If if were to be implemented, it would be deadlock-prone in 1020 * much the manner of {@link #suspend}. If the target thread held 1021 * a lock protecting a critical system resource when it was 1022 * destroyed, no thread could ever access this resource again. 1023 * If another thread ever attempted to lock this resource, deadlock 1024 * would result. Such deadlocks typically manifest themselves as 1025 * "frozen" processes. For more information, see 1026 * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html"> 1027 * Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 1028 * @throws NoSuchMethodError always 1029 */ 1030 @Deprecated 1031 public void destroy() { 1032 throw new NoSuchMethodError(); 1033 } 1034 1035 /** 1036 * Tests if this thread is alive. A thread is alive if it has 1037 * been started and has not yet died. 1038 * 1039 * @return <code>true</code> if this thread is alive; 1040 * <code>false</code> otherwise. 1041 */ 1042 public final native boolean isAlive(); 1043 1044 /** 1045 * Suspends this thread. 1046 * <p> 1047 * First, the <code>checkAccess</code> method of this thread is called 1048 * with no arguments. This may result in throwing a 1049 * <code>SecurityException </code>(in the current thread). 1050 * <p> 1051 * If the thread is alive, it is suspended and makes no further 1052 * progress unless and until it is resumed. 1053 * 1054 * @exception SecurityException if the current thread cannot modify 1055 * this thread. 1056 * @see #checkAccess 1057 * @deprecated This method has been deprecated, as it is 1058 * inherently deadlock-prone. If the target thread holds a lock on the 1059 * monitor protecting a critical system resource when it is suspended, no 1060 * thread can access this resource until the target thread is resumed. If 1061 * the thread that would resume the target thread attempts to lock this 1062 * monitor prior to calling <code>resume</code>, deadlock results. Such 1063 * deadlocks typically manifest themselves as "frozen" processes. 1064 * For more information, see 1065 * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why 1066 * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 1067 */ 1068 @Deprecated 1069 public final void suspend() { 1070 checkAccess(); 1071 suspend0(); 1072 } 1073 1074 /** 1075 * Resumes a suspended thread. 1076 * <p> 1077 * First, the <code>checkAccess</code> method of this thread is called 1078 * with no arguments. This may result in throwing a 1079 * <code>SecurityException</code> (in the current thread). 1080 * <p> 1081 * If the thread is alive but suspended, it is resumed and is 1082 * permitted to make progress in its execution. 1083 * 1084 * @exception SecurityException if the current thread cannot modify this 1085 * thread. 1086 * @see #checkAccess 1087 * @see #suspend() 1088 * @deprecated This method exists solely for use with {@link #suspend}, 1089 * which has been deprecated because it is deadlock-prone. 1090 * For more information, see 1091 * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why 1092 * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 1093 */ 1094 @Deprecated 1095 public final void resume() { 1096 checkAccess(); 1097 resume0(); 1098 } 1099 1100 /** 1101 * Changes the priority of this thread. 1102 * <p> 1103 * First the <code>checkAccess</code> method of this thread is called 1104 * with no arguments. This may result in throwing a 1105 * <code>SecurityException</code>. 1106 * <p> 1107 * Otherwise, the priority of this thread is set to the smaller of 1108 * the specified <code>newPriority</code> and the maximum permitted 1109 * priority of the thread's thread group. 1110 * 1111 * @param newPriority priority to set this thread to 1112 * @exception IllegalArgumentException If the priority is not in the 1113 * range <code>MIN_PRIORITY</code> to 1114 * <code>MAX_PRIORITY</code>. 1115 * @exception SecurityException if the current thread cannot modify 1116 * this thread. 1117 * @see #getPriority 1118 * @see #checkAccess() 1119 * @see #getThreadGroup() 1120 * @see #MAX_PRIORITY 1121 * @see #MIN_PRIORITY 1122 * @see ThreadGroup#getMaxPriority() 1123 */ 1124 public final void setPriority(int newPriority) { 1125 ThreadGroup g; 1126 checkAccess(); 1127 if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { 1128 throw new IllegalArgumentException(); 1129 } 1130 if((g = getThreadGroup()) != null) { 1131 if (newPriority > g.getMaxPriority()) { 1132 newPriority = g.getMaxPriority(); 1133 } 1134 setPriority0(priority = newPriority); 1135 } 1136 } 1137 1138 /** 1139 * Returns this thread's priority. 1140 * 1141 * @return this thread's priority. 1142 * @see #setPriority 1143 */ 1144 public final int getPriority() { 1145 return priority; 1146 } 1147 1148 /** 1149 * Changes the name of this thread to be equal to the argument 1150 * <code>name</code>. 1151 * <p> 1152 * First the <code>checkAccess</code> method of this thread is called 1153 * with no arguments. This may result in throwing a 1154 * <code>SecurityException</code>. 1155 * 1156 * @param name the new name for this thread. 1157 * @exception SecurityException if the current thread cannot modify this 1158 * thread. 1159 * @see #getName 1160 * @see #checkAccess() 1161 */ 1162 public final void setName(String name) { 1163 checkAccess(); 1164 this.name = name.toCharArray(); 1165 } 1166 1167 /** 1168 * Returns this thread's name. 1169 * 1170 * @return this thread's name. 1171 * @see #setName(String) 1172 */ 1173 public final String getName() { 1174 return String.valueOf(name); 1175 } 1176 1177 /** 1178 * Returns the thread group to which this thread belongs. 1179 * This method returns null if this thread has died 1180 * (been stopped). 1181 * 1182 * @return this thread's thread group. 1183 */ 1184 public final ThreadGroup getThreadGroup() { 1185 return group; 1186 } 1187 1188 /** 1189 * Returns an estimate of the number of active threads in the current 1190 * thread's {@linkplain java.lang.ThreadGroup thread group} and its 1191 * subgroups. Recursively iterates over all subgroups in the current 1192 * thread's thread group. 1193 * 1194 * <p> The value returned is only an estimate because the number of 1195 * threads may change dynamically while this method traverses internal 1196 * data structures, and might be affected by the presence of certain 1197 * system threads. This method is intended primarily for debugging 1198 * and monitoring purposes. 1199 * 1200 * @return an estimate of the number of active threads in the current 1201 * thread's thread group and in any other thread group that 1202 * has the current thread's thread group as an ancestor 1203 */ 1204 public static int activeCount() { 1205 return currentThread().getThreadGroup().activeCount(); 1206 } 1207 1208 /** 1209 * Copies into the specified array every active thread in the current 1210 * thread's thread group and its subgroups. This method simply 1211 * invokes the {@link java.lang.ThreadGroup#enumerate(Thread[])} 1212 * method of the current thread's thread group. 1213 * 1214 * <p> An application might use the {@linkplain #activeCount activeCount} 1215 * method to get an estimate of how big the array should be, however 1216 * <i>if the array is too short to hold all the threads, the extra threads 1217 * are silently ignored.</i> If it is critical to obtain every active 1218 * thread in the current thread's thread group and its subgroups, the 1219 * invoker should verify that the returned int value is strictly less 1220 * than the length of {@code tarray}. 1221 * 1222 * <p> Due to the inherent race condition in this method, it is recommended 1223 * that the method only be used for debugging and monitoring purposes. 1224 * 1225 * @param tarray 1226 * an array into which to put the list of threads 1227 * 1228 * @return the number of threads put into the array 1229 * 1230 * @throws SecurityException 1231 * if {@link java.lang.ThreadGroup#checkAccess} determines that 1232 * the current thread cannot access its thread group 1233 */ 1234 public static int enumerate(Thread tarray[]) { 1235 return currentThread().getThreadGroup().enumerate(tarray); 1236 } 1237 1238 /** 1239 * Counts the number of stack frames in this thread. The thread must 1240 * be suspended. 1241 * 1242 * @return the number of stack frames in this thread. 1243 * @exception IllegalThreadStateException if this thread is not 1244 * suspended. 1245 * @deprecated The definition of this call depends on {@link #suspend}, 1246 * which is deprecated. Further, the results of this call 1247 * were never well-defined. 1248 */ 1249 @Deprecated 1250 public native int countStackFrames(); 1251 1252 /** 1253 * Waits at most {@code millis} milliseconds for this thread to 1254 * die. A timeout of {@code 0} means to wait forever. 1255 * 1256 * <p> This implementation uses a loop of {@code this.wait} calls 1257 * conditioned on {@code this.isAlive}. As a thread terminates the 1258 * {@code this.notifyAll} method is invoked. It is recommended that 1259 * applications not use {@code wait}, {@code notify}, or 1260 * {@code notifyAll} on {@code Thread} instances. 1261 * 1262 * @param millis 1263 * the time to wait in milliseconds 1264 * 1265 * @throws IllegalArgumentException 1266 * if the value of {@code millis} is negative 1267 * 1268 * @throws InterruptedException 1269 * if any thread has interrupted the current thread. The 1270 * <i>interrupted status</i> of the current thread is 1271 * cleared when this exception is thrown. 1272 */ 1273 public final synchronized void join(long millis) 1274 throws InterruptedException { 1275 long base = System.currentTimeMillis(); 1276 long now = 0; 1277 1278 if (millis < 0) { 1279 throw new IllegalArgumentException("timeout value is negative"); 1280 } 1281 1282 if (millis == 0) { 1283 while (isAlive()) { 1284 wait(0); 1285 } 1286 } else { 1287 while (isAlive()) { 1288 long delay = millis - now; 1289 if (delay <= 0) { 1290 break; 1291 } 1292 wait(delay); 1293 now = System.currentTimeMillis() - base; 1294 } 1295 } 1296 } 1297 1298 /** 1299 * Waits at most {@code millis} milliseconds plus 1300 * {@code nanos} nanoseconds for this thread to die. 1301 * 1302 * <p> This implementation uses a loop of {@code this.wait} calls 1303 * conditioned on {@code this.isAlive}. As a thread terminates the 1304 * {@code this.notifyAll} method is invoked. It is recommended that 1305 * applications not use {@code wait}, {@code notify}, or 1306 * {@code notifyAll} on {@code Thread} instances. 1307 * 1308 * @param millis 1309 * the time to wait in milliseconds 1310 * 1311 * @param nanos 1312 * {@code 0-999999} additional nanoseconds to wait 1313 * 1314 * @throws IllegalArgumentException 1315 * if the value of {@code millis} is negative, or the value 1316 * of {@code nanos} is not in the range {@code 0-999999} 1317 * 1318 * @throws InterruptedException 1319 * if any thread has interrupted the current thread. The 1320 * <i>interrupted status</i> of the current thread is 1321 * cleared when this exception is thrown. 1322 */ 1323 public final synchronized void join(long millis, int nanos) 1324 throws InterruptedException { 1325 1326 if (millis < 0) { 1327 throw new IllegalArgumentException("timeout value is negative"); 1328 } 1329 1330 if (nanos < 0 || nanos > 999999) { 1331 throw new IllegalArgumentException( 1332 "nanosecond timeout value out of range"); 1333 } 1334 1335 if (nanos >= 500000 || (nanos != 0 && millis == 0)) { 1336 millis++; 1337 } 1338 1339 join(millis); 1340 } 1341 1342 /** 1343 * Waits for this thread to die. 1344 * 1345 * <p> An invocation of this method behaves in exactly the same 1346 * way as the invocation 1347 * 1348 * <blockquote> 1349 * {@linkplain #join(long) join}{@code (0)} 1350 * </blockquote> 1351 * 1352 * @throws InterruptedException 1353 * if any thread has interrupted the current thread. The 1354 * <i>interrupted status</i> of the current thread is 1355 * cleared when this exception is thrown. 1356 */ 1357 public final void join() throws InterruptedException { 1358 join(0); 1359 } 1360 1361 /** 1362 * Prints a stack trace of the current thread to the standard error stream. 1363 * This method is used only for debugging. 1364 * 1365 * @see Throwable#printStackTrace() 1366 */ 1367 public static void dumpStack() { 1368 new Exception("Stack trace").printStackTrace(); 1369 } 1370 1371 /** 1372 * Marks this thread as either a {@linkplain #isDaemon daemon} thread 1373 * or a user thread. The Java Virtual Machine exits when the only 1374 * threads running are all daemon threads. 1375 * 1376 * <p> This method must be invoked before the thread is started. 1377 * 1378 * @param on 1379 * if {@code true}, marks this thread as a daemon thread 1380 * 1381 * @throws IllegalThreadStateException 1382 * if this thread is {@linkplain #isAlive alive} 1383 * 1384 * @throws SecurityException 1385 * if {@link #checkAccess} determines that the current 1386 * thread cannot modify this thread 1387 */ 1388 public final void setDaemon(boolean on) { 1389 checkAccess(); 1390 if (isAlive()) { 1391 throw new IllegalThreadStateException(); 1392 } 1393 daemon = on; 1394 } 1395 1396 /** 1397 * Tests if this thread is a daemon thread. 1398 * 1399 * @return <code>true</code> if this thread is a daemon thread; 1400 * <code>false</code> otherwise. 1401 * @see #setDaemon(boolean) 1402 */ 1403 public final boolean isDaemon() { 1404 return daemon; 1405 } 1406 1407 /** 1408 * Determines if the currently running thread has permission to 1409 * modify this thread. 1410 * <p> 1411 * If there is a security manager, its <code>checkAccess</code> method 1412 * is called with this thread as its argument. This may result in 1413 * throwing a <code>SecurityException</code>. 1414 * 1415 * @exception SecurityException if the current thread is not allowed to 1416 * access this thread. 1417 * @see SecurityManager#checkAccess(Thread) 1418 */ 1419 public final void checkAccess() { 1420 SecurityManager security = System.getSecurityManager(); 1421 if (security != null) { 1422 security.checkAccess(this); 1423 } 1424 } 1425 1426 /** 1427 * Returns a string representation of this thread, including the 1428 * thread's name, priority, and thread group. 1429 * 1430 * @return a string representation of this thread. 1431 */ 1432 public String toString() { 1433 ThreadGroup group = getThreadGroup(); 1434 if (group != null) { 1435 return "Thread[" + getName() + "," + getPriority() + "," + 1436 group.getName() + "]"; 1437 } else { 1438 return "Thread[" + getName() + "," + getPriority() + "," + 1439 "" + "]"; 1440 } 1441 } 1442 1443 /** 1444 * Returns the context ClassLoader for this Thread. The context 1445 * ClassLoader is provided by the creator of the thread for use 1446 * by code running in this thread when loading classes and resources. 1447 * If not {@linkplain #setContextClassLoader set}, the default is the 1448 * ClassLoader context of the parent Thread. The context ClassLoader of the 1449 * primordial thread is typically set to the class loader used to load the 1450 * application. 1451 * 1452 * <p>If a security manager is present, and the invoker's class loader is not 1453 * {@code null} and is not the same as or an ancestor of the context class 1454 * loader, then this method invokes the security manager's {@link 1455 * SecurityManager#checkPermission(java.security.Permission) checkPermission} 1456 * method with a {@link RuntimePermission RuntimePermission}{@code 1457 * ("getClassLoader")} permission to verify that retrieval of the context 1458 * class loader is permitted. 1459 * 1460 * @return the context ClassLoader for this Thread, or {@code null} 1461 * indicating the system class loader (or, failing that, the 1462 * bootstrap class loader) 1463 * 1464 * @throws SecurityException 1465 * if the current thread cannot get the context ClassLoader 1466 * 1467 * @since 1.2 1468 */ 1469 public ClassLoader getContextClassLoader() { 1470 if (contextClassLoader == null) 1471 return null; 1472 SecurityManager sm = System.getSecurityManager(); 1473 if (sm != null) { 1474 ClassLoader ccl = ClassLoader.getCallerClassLoader(); 1475 if (ccl != null && ccl != contextClassLoader && 1476 !contextClassLoader.isAncestor(ccl)) { 1477 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); 1478 } 1479 } 1480 return contextClassLoader; 1481 } 1482 1483 /** 1484 * Sets the context ClassLoader for this Thread. The context 1485 * ClassLoader can be set when a thread is created, and allows 1486 * the creator of the thread to provide the appropriate class loader, 1487 * through {@code getContextClassLoader}, to code running in the thread 1488 * when loading classes and resources. 1489 * 1490 * <p>If a security manager is present, its {@link 1491 * SecurityManager#checkPermission(java.security.Permission) checkPermission} 1492 * method is invoked with a {@link RuntimePermission RuntimePermission}{@code 1493 * ("setContextClassLoader")} permission to see if setting the context 1494 * ClassLoader is permitted. 1495 * 1496 * @param cl 1497 * the context ClassLoader for this Thread, or null indicating the 1498 * system class loader (or, failing that, the bootstrap class loader) 1499 * 1500 * @throws SecurityException 1501 * if the current thread cannot set the context ClassLoader 1502 * 1503 * @since 1.2 1504 */ 1505 public void setContextClassLoader(ClassLoader cl) { 1506 SecurityManager sm = System.getSecurityManager(); 1507 if (sm != null) { 1508 sm.checkPermission(new RuntimePermission("setContextClassLoader")); 1509 } 1510 contextClassLoader = cl; 1511 } 1512 1513 /** 1514 * Returns <tt>true</tt> if and only if the current thread holds the 1515 * monitor lock on the specified object. 1516 * 1517 * <p>This method is designed to allow a program to assert that 1518 * the current thread already holds a specified lock: 1519 * <pre> 1520 * assert Thread.holdsLock(obj); 1521 * </pre> 1522 * 1523 * @param obj the object on which to test lock ownership 1524 * @throws NullPointerException if obj is <tt>null</tt> 1525 * @return <tt>true</tt> if the current thread holds the monitor lock on 1526 * the specified object. 1527 * @since 1.4 1528 */ 1529 public static native boolean holdsLock(Object obj); 1530 1531 private static final StackTraceElement[] EMPTY_STACK_TRACE 1532 = new StackTraceElement[0]; 1533 1534 /** 1535 * Returns an array of stack trace elements representing the stack dump 1536 * of this thread. This method will return a zero-length array if 1537 * this thread has not started, has started but has not yet been 1538 * scheduled to run by the system, or has terminated. 1539 * If the returned array is of non-zero length then the first element of 1540 * the array represents the top of the stack, which is the most recent 1541 * method invocation in the sequence. The last element of the array 1542 * represents the bottom of the stack, which is the least recent method 1543 * invocation in the sequence. 1544 * 1545 * <p>If there is a security manager, and this thread is not 1546 * the current thread, then the security manager's 1547 * <tt>checkPermission</tt> method is called with a 1548 * <tt>RuntimePermission("getStackTrace")</tt> permission 1549 * to see if it's ok to get the stack trace. 1550 * 1551 * <p>Some virtual machines may, under some circumstances, omit one 1552 * or more stack frames from the stack trace. In the extreme case, 1553 * a virtual machine that has no stack trace information concerning 1554 * this thread is permitted to return a zero-length array from this 1555 * method. 1556 * 1557 * @return an array of <tt>StackTraceElement</tt>, 1558 * each represents one stack frame. 1559 * 1560 * @throws SecurityException 1561 * if a security manager exists and its 1562 * <tt>checkPermission</tt> method doesn't allow 1563 * getting the stack trace of thread. 1564 * @see SecurityManager#checkPermission 1565 * @see RuntimePermission 1566 * @see Throwable#getStackTrace 1567 * 1568 * @since 1.5 1569 */ 1570 public StackTraceElement[] getStackTrace() { 1571 if (this != Thread.currentThread()) { 1572 // check for getStackTrace permission 1573 SecurityManager security = System.getSecurityManager(); 1574 if (security != null) { 1575 security.checkPermission( 1576 SecurityConstants.GET_STACK_TRACE_PERMISSION); 1577 } 1578 // optimization so we do not call into the vm for threads that 1579 // have not yet started or have terminated 1580 if (!isAlive()) { 1581 return EMPTY_STACK_TRACE; 1582 } 1583 StackTraceElement[][] stackTraceArray = dumpThreads(new Thread[] {this}); 1584 StackTraceElement[] stackTrace = stackTraceArray[0]; 1585 // a thread that was alive during the previous isAlive call may have 1586 // since terminated, therefore not having a stacktrace. 1587 if (stackTrace == null) { 1588 stackTrace = EMPTY_STACK_TRACE; 1589 } 1590 return stackTrace; 1591 } else { 1592 // Don't need JVM help for current thread 1593 return (new Exception()).getStackTrace(); 1594 } 1595 } 1596 1597 /** 1598 * Returns a map of stack traces for all live threads. 1599 * The map keys are threads and each map value is an array of 1600 * <tt>StackTraceElement</tt> that represents the stack dump 1601 * of the corresponding <tt>Thread</tt>. 1602 * The returned stack traces are in the format specified for 1603 * the {@link #getStackTrace getStackTrace} method. 1604 * 1605 * <p>The threads may be executing while this method is called. 1606 * The stack trace of each thread only represents a snapshot and 1607 * each stack trace may be obtained at different time. A zero-length 1608 * array will be returned in the map value if the virtual machine has 1609 * no stack trace information about a thread. 1610 * 1611 * <p>If there is a security manager, then the security manager's 1612 * <tt>checkPermission</tt> method is called with a 1613 * <tt>RuntimePermission("getStackTrace")</tt> permission as well as 1614 * <tt>RuntimePermission("modifyThreadGroup")</tt> permission 1615 * to see if it is ok to get the stack trace of all threads. 1616 * 1617 * @return a <tt>Map</tt> from <tt>Thread</tt> to an array of 1618 * <tt>StackTraceElement</tt> that represents the stack trace of 1619 * the corresponding thread. 1620 * 1621 * @throws SecurityException 1622 * if a security manager exists and its 1623 * <tt>checkPermission</tt> method doesn't allow 1624 * getting the stack trace of thread. 1625 * @see #getStackTrace 1626 * @see SecurityManager#checkPermission 1627 * @see RuntimePermission 1628 * @see Throwable#getStackTrace 1629 * 1630 * @since 1.5 1631 */ 1632 public static Map<Thread, StackTraceElement[]> getAllStackTraces() { 1633 // check for getStackTrace permission 1634 SecurityManager security = System.getSecurityManager(); 1635 if (security != null) { 1636 security.checkPermission( 1637 SecurityConstants.GET_STACK_TRACE_PERMISSION); 1638 security.checkPermission( 1639 SecurityConstants.MODIFY_THREADGROUP_PERMISSION); 1640 } 1641 1642 // Get a snapshot of the list of all threads 1643 Thread[] threads = getThreads(); 1644 StackTraceElement[][] traces = dumpThreads(threads); 1645 Map<Thread, StackTraceElement[]> m 1646 = new HashMap<Thread, StackTraceElement[]>(threads.length); 1647 for (int i = 0; i < threads.length; i++) { 1648 StackTraceElement[] stackTrace = traces[i]; 1649 if (stackTrace != null) { 1650 m.put(threads[i], stackTrace); 1651 } 1652 // else terminated so we don't put it in the map 1653 } 1654 return m; 1655 } 1656 1657 1658 private static final RuntimePermission SUBCLASS_IMPLEMENTATION_PERMISSION = 1659 new RuntimePermission("enableContextClassLoaderOverride"); 1660 1661 /** cache of subclass security audit results */ 1662 /* Replace with ConcurrentReferenceHashMap when/if it appears in a future 1663 * release */ 1664 private static class Caches { 1665 /** cache of subclass security audit results */ 1666 static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits = 1667 new ConcurrentHashMap<WeakClassKey,Boolean>(); 1668 1669 /** queue for WeakReferences to audited subclasses */ 1670 static final ReferenceQueue<Class<?>> subclassAuditsQueue = 1671 new ReferenceQueue<Class<?>>(); 1672 } 1673 1674 /** 1675 * Verifies that this (possibly subclass) instance can be constructed 1676 * without violating security constraints: the subclass must not override 1677 * security-sensitive non-final methods, or else the 1678 * "enableContextClassLoaderOverride" RuntimePermission is checked. 1679 */ 1680 private static boolean isCCLOverridden(Class cl) { 1681 if (cl == Thread.class) 1682 return false; 1683 1684 processQueue(Caches.subclassAuditsQueue, Caches.subclassAudits); 1685 WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue); 1686 Boolean result = Caches.subclassAudits.get(key); 1687 if (result == null) { 1688 result = Boolean.valueOf(auditSubclass(cl)); 1689 Caches.subclassAudits.putIfAbsent(key, result); 1690 } 1691 1692 return result.booleanValue(); 1693 } 1694 1695 /** 1696 * Performs reflective checks on given subclass to verify that it doesn't 1697 * override security-sensitive non-final methods. Returns true if the 1698 * subclass overrides any of the methods, false otherwise. 1699 */ 1700 private static boolean auditSubclass(final Class subcl) { 1701 Boolean result = AccessController.doPrivileged( 1702 new PrivilegedAction<Boolean>() { 1703 public Boolean run() { 1704 for (Class cl = subcl; 1705 cl != Thread.class; 1706 cl = cl.getSuperclass()) 1707 { 1708 try { 1709 cl.getDeclaredMethod("getContextClassLoader", new Class[0]); 1710 return Boolean.TRUE; 1711 } catch (NoSuchMethodException ex) { 1712 } 1713 try { 1714 Class[] params = {ClassLoader.class}; 1715 cl.getDeclaredMethod("setContextClassLoader", params); 1716 return Boolean.TRUE; 1717 } catch (NoSuchMethodException ex) { 1718 } 1719 } 1720 return Boolean.FALSE; 1721 } 1722 } 1723 ); 1724 return result.booleanValue(); 1725 } 1726 1727 private native static StackTraceElement[][] dumpThreads(Thread[] threads); 1728 private native static Thread[] getThreads(); 1729 1730 /** 1731 * Returns the identifier of this Thread. The thread ID is a positive 1732 * <tt>long</tt> number generated when this thread was created. 1733 * The thread ID is unique and remains unchanged during its lifetime. 1734 * When a thread is terminated, this thread ID may be reused. 1735 * 1736 * @return this thread's ID. 1737 * @since 1.5 1738 */ 1739 public long getId() { 1740 return tid; 1741 } 1742 1743 /** 1744 * A thread state. A thread can be in one of the following states: 1745 * <ul> 1746 * <li>{@link #NEW}<br> 1747 * A thread that has not yet started is in this state. 1748 * </li> 1749 * <li>{@link #RUNNABLE}<br> 1750 * A thread executing in the Java virtual machine is in this state. 1751 * </li> 1752 * <li>{@link #BLOCKED}<br> 1753 * A thread that is blocked waiting for a monitor lock 1754 * is in this state. 1755 * </li> 1756 * <li>{@link #WAITING}<br> 1757 * A thread that is waiting indefinitely for another thread to 1758 * perform a particular action is in this state. 1759 * </li> 1760 * <li>{@link #TIMED_WAITING}<br> 1761 * A thread that is waiting for another thread to perform an action 1762 * for up to a specified waiting time is in this state. 1763 * </li> 1764 * <li>{@link #TERMINATED}<br> 1765 * A thread that has exited is in this state. 1766 * </li> 1767 * </ul> 1768 * 1769 * <p> 1770 * A thread can be in only one state at a given point in time. 1771 * These states are virtual machine states which do not reflect 1772 * any operating system thread states. 1773 * 1774 * @since 1.5 1775 * @see #getState 1776 */ 1777 public enum State { 1778 /** 1779 * Thread state for a thread which has not yet started. 1780 */ 1781 NEW, 1782 1783 /** 1784 * Thread state for a runnable thread. A thread in the runnable 1785 * state is executing in the Java virtual machine but it may 1786 * be waiting for other resources from the operating system 1787 * such as processor. 1788 */ 1789 RUNNABLE, 1790 1791 /** 1792 * Thread state for a thread blocked waiting for a monitor lock. 1793 * A thread in the blocked state is waiting for a monitor lock 1794 * to enter a synchronized block/method or 1795 * reenter a synchronized block/method after calling 1796 * {@link Object#wait() Object.wait}. 1797 */ 1798 BLOCKED, 1799 1800 /** 1801 * Thread state for a waiting thread. 1802 * A thread is in the waiting state due to calling one of the 1803 * following methods: 1804 * <ul> 1805 * <li>{@link Object#wait() Object.wait} with no timeout</li> 1806 * <li>{@link #join() Thread.join} with no timeout</li> 1807 * <li>{@link LockSupport#park() LockSupport.park}</li> 1808 * </ul> 1809 * 1810 * <p>A thread in the waiting state is waiting for another thread to 1811 * perform a particular action. 1812 * 1813 * For example, a thread that has called <tt>Object.wait()</tt> 1814 * on an object is waiting for another thread to call 1815 * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on 1816 * that object. A thread that has called <tt>Thread.join()</tt> 1817 * is waiting for a specified thread to terminate. 1818 */ 1819 WAITING, 1820 1821 /** 1822 * Thread state for a waiting thread with a specified waiting time. 1823 * A thread is in the timed waiting state due to calling one of 1824 * the following methods with a specified positive waiting time: 1825 * <ul> 1826 * <li>{@link #sleep Thread.sleep}</li> 1827 * <li>{@link Object#wait(long) Object.wait} with timeout</li> 1828 * <li>{@link #join(long) Thread.join} with timeout</li> 1829 * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> 1830 * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> 1831 * </ul> 1832 */ 1833 TIMED_WAITING, 1834 1835 /** 1836 * Thread state for a terminated thread. 1837 * The thread has completed execution. 1838 */ 1839 TERMINATED; 1840 } 1841 1842 /** 1843 * Returns the state of this thread. 1844 * This method is designed for use in monitoring of the system state, 1845 * not for synchronization control. 1846 * 1847 * @return this thread's state. 1848 * @since 1.5 1849 */ 1850 public State getState() { 1851 // get current thread state 1852 return sun.misc.VM.toThreadState(threadStatus); 1853 } 1854 1855 // Added in JSR-166 1856 1857 /** 1858 * Interface for handlers invoked when a <tt>Thread</tt> abruptly 1859 * terminates due to an uncaught exception. 1860 * <p>When a thread is about to terminate due to an uncaught exception 1861 * the Java Virtual Machine will query the thread for its 1862 * <tt>UncaughtExceptionHandler</tt> using 1863 * {@link #getUncaughtExceptionHandler} and will invoke the handler's 1864 * <tt>uncaughtException</tt> method, passing the thread and the 1865 * exception as arguments. 1866 * If a thread has not had its <tt>UncaughtExceptionHandler</tt> 1867 * explicitly set, then its <tt>ThreadGroup</tt> object acts as its 1868 * <tt>UncaughtExceptionHandler</tt>. If the <tt>ThreadGroup</tt> object 1869 * has no 1870 * special requirements for dealing with the exception, it can forward 1871 * the invocation to the {@linkplain #getDefaultUncaughtExceptionHandler 1872 * default uncaught exception handler}. 1873 * 1874 * @see #setDefaultUncaughtExceptionHandler 1875 * @see #setUncaughtExceptionHandler 1876 * @see ThreadGroup#uncaughtException 1877 * @since 1.5 1878 */ 1879 public interface UncaughtExceptionHandler { 1880 /** 1881 * Method invoked when the given thread terminates due to the 1882 * given uncaught exception. 1883 * <p>Any exception thrown by this method will be ignored by the 1884 * Java Virtual Machine. 1885 * @param t the thread 1886 * @param e the exception 1887 */ 1888 void uncaughtException(Thread t, Throwable e); 1889 } 1890 1891 // null unless explicitly set 1892 private volatile UncaughtExceptionHandler uncaughtExceptionHandler; 1893 1894 // null unless explicitly set 1895 private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler; 1896 1897 /** 1898 * Set the default handler invoked when a thread abruptly terminates 1899 * due to an uncaught exception, and no other handler has been defined 1900 * for that thread. 1901 * 1902 * <p>Uncaught exception handling is controlled first by the thread, then 1903 * by the thread's {@link ThreadGroup} object and finally by the default 1904 * uncaught exception handler. If the thread does not have an explicit 1905 * uncaught exception handler set, and the thread's thread group 1906 * (including parent thread groups) does not specialize its 1907 * <tt>uncaughtException</tt> method, then the default handler's 1908 * <tt>uncaughtException</tt> method will be invoked. 1909 * <p>By setting the default uncaught exception handler, an application 1910 * can change the way in which uncaught exceptions are handled (such as 1911 * logging to a specific device, or file) for those threads that would 1912 * already accept whatever "default" behavior the system 1913 * provided. 1914 * 1915 * <p>Note that the default uncaught exception handler should not usually 1916 * defer to the thread's <tt>ThreadGroup</tt> object, as that could cause 1917 * infinite recursion. 1918 * 1919 * @param eh the object to use as the default uncaught exception handler. 1920 * If <tt>null</tt> then there is no default handler. 1921 * 1922 * @throws SecurityException if a security manager is present and it 1923 * denies <tt>{@link RuntimePermission} 1924 * ("setDefaultUncaughtExceptionHandler")</tt> 1925 * 1926 * @see #setUncaughtExceptionHandler 1927 * @see #getUncaughtExceptionHandler 1928 * @see ThreadGroup#uncaughtException 1929 * @since 1.5 1930 */ 1931 public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) { 1932 SecurityManager sm = System.getSecurityManager(); 1933 if (sm != null) { 1934 sm.checkPermission( 1935 new RuntimePermission("setDefaultUncaughtExceptionHandler") 1936 ); 1937 } 1938 1939 defaultUncaughtExceptionHandler = eh; 1940 } 1941 1942 /** 1943 * Returns the default handler invoked when a thread abruptly terminates 1944 * due to an uncaught exception. If the returned value is <tt>null</tt>, 1945 * there is no default. 1946 * @since 1.5 1947 * @see #setDefaultUncaughtExceptionHandler 1948 */ 1949 public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){ 1950 return defaultUncaughtExceptionHandler; 1951 } 1952 1953 /** 1954 * Returns the handler invoked when this thread abruptly terminates 1955 * due to an uncaught exception. If this thread has not had an 1956 * uncaught exception handler explicitly set then this thread's 1957 * <tt>ThreadGroup</tt> object is returned, unless this thread 1958 * has terminated, in which case <tt>null</tt> is returned. 1959 * @since 1.5 1960 */ 1961 public UncaughtExceptionHandler getUncaughtExceptionHandler() { 1962 return uncaughtExceptionHandler != null ? 1963 uncaughtExceptionHandler : group; 1964 } 1965 1966 /** 1967 * Set the handler invoked when this thread abruptly terminates 1968 * due to an uncaught exception. 1969 * <p>A thread can take full control of how it responds to uncaught 1970 * exceptions by having its uncaught exception handler explicitly set. 1971 * If no such handler is set then the thread's <tt>ThreadGroup</tt> 1972 * object acts as its handler. 1973 * @param eh the object to use as this thread's uncaught exception 1974 * handler. If <tt>null</tt> then this thread has no explicit handler. 1975 * @throws SecurityException if the current thread is not allowed to 1976 * modify this thread. 1977 * @see #setDefaultUncaughtExceptionHandler 1978 * @see ThreadGroup#uncaughtException 1979 * @since 1.5 1980 */ 1981 public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) { 1982 checkAccess(); 1983 uncaughtExceptionHandler = eh; 1984 } 1985 1986 /** 1987 * Dispatch an uncaught exception to the handler. This method is 1988 * intended to be called only by the JVM. 1989 */ 1990 private void dispatchUncaughtException(Throwable e) { 1991 getUncaughtExceptionHandler().uncaughtException(this, e); 1992 } 1993 1994 /** 1995 * Removes from the specified map any keys that have been enqueued 1996 * on the specified reference queue. 1997 */ 1998 static void processQueue(ReferenceQueue<Class<?>> queue, 1999 ConcurrentMap<? extends 2000 WeakReference<Class<?>>, ?> map) 2001 { 2002 Reference<? extends Class<?>> ref; 2003 while((ref = queue.poll()) != null) { 2004 map.remove(ref); 2005 } 2006 } 2007 2008 /** 2009 * Weak key for Class objects. 2010 **/ 2011 static class WeakClassKey extends WeakReference<Class<?>> { 2012 /** 2013 * saved value of the referent's identity hash code, to maintain 2014 * a consistent hash code after the referent has been cleared 2015 */ 2016 private final int hash; 2017 2018 /** 2019 * Create a new WeakClassKey to the given object, registered 2020 * with a queue. 2021 */ 2022 WeakClassKey(Class<?> cl, ReferenceQueue<Class<?>> refQueue) { 2023 super(cl, refQueue); 2024 hash = System.identityHashCode(cl); 2025 } 2026 2027 /** 2028 * Returns the identity hash code of the original referent. 2029 */ 2030 @Override 2031 public int hashCode() { 2032 return hash; 2033 } 2034 2035 /** 2036 * Returns true if the given object is this identical 2037 * WeakClassKey instance, or, if this object's referent has not 2038 * been cleared, if the given object is another WeakClassKey 2039 * instance with the identical non-null referent as this one. 2040 */ 2041 @Override 2042 public boolean equals(Object obj) { 2043 if (obj == this) 2044 return true; 2045 2046 if (obj instanceof WeakClassKey) { 2047 Object referent = get(); 2048 return (referent != null) && 2049 (referent == ((WeakClassKey) obj).get()); 2050 } else { 2051 return false; 2052 } 2053 } 2054 } 2055 2056 /* Some private helper methods */ 2057 private native void setPriority0(int newPriority); 2058 private native void stop0(Object o); 2059 private native void suspend0(); 2060 private native void resume0(); 2061 private native void interrupt0(); 2062 }