1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/lang/Thread.java Sat Sep 07 13:51:24 2013 +0200
1.3 @@ -0,0 +1,2035 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +import java.lang.ref.Reference;
1.32 +import java.lang.ref.ReferenceQueue;
1.33 +import java.lang.ref.WeakReference;
1.34 +import java.security.AccessController;
1.35 +import java.security.AccessControlContext;
1.36 +import java.security.PrivilegedAction;
1.37 +import java.util.Map;
1.38 +import java.util.HashMap;
1.39 +import java.util.concurrent.ConcurrentHashMap;
1.40 +import java.util.concurrent.ConcurrentMap;
1.41 +import java.util.concurrent.locks.LockSupport;
1.42 +import sun.nio.ch.Interruptible;
1.43 +import sun.security.util.SecurityConstants;
1.44 +
1.45 +
1.46 +/**
1.47 + * A <i>thread</i> is a thread of execution in a program. The Java
1.48 + * Virtual Machine allows an application to have multiple threads of
1.49 + * execution running concurrently.
1.50 + * <p>
1.51 + * Every thread has a priority. Threads with higher priority are
1.52 + * executed in preference to threads with lower priority. Each thread
1.53 + * may or may not also be marked as a daemon. When code running in
1.54 + * some thread creates a new <code>Thread</code> object, the new
1.55 + * thread has its priority initially set equal to the priority of the
1.56 + * creating thread, and is a daemon thread if and only if the
1.57 + * creating thread is a daemon.
1.58 + * <p>
1.59 + * When a Java Virtual Machine starts up, there is usually a single
1.60 + * non-daemon thread (which typically calls the method named
1.61 + * <code>main</code> of some designated class). The Java Virtual
1.62 + * Machine continues to execute threads until either of the following
1.63 + * occurs:
1.64 + * <ul>
1.65 + * <li>The <code>exit</code> method of class <code>Runtime</code> has been
1.66 + * called and the security manager has permitted the exit operation
1.67 + * to take place.
1.68 + * <li>All threads that are not daemon threads have died, either by
1.69 + * returning from the call to the <code>run</code> method or by
1.70 + * throwing an exception that propagates beyond the <code>run</code>
1.71 + * method.
1.72 + * </ul>
1.73 + * <p>
1.74 + * There are two ways to create a new thread of execution. One is to
1.75 + * declare a class to be a subclass of <code>Thread</code>. This
1.76 + * subclass should override the <code>run</code> method of class
1.77 + * <code>Thread</code>. An instance of the subclass can then be
1.78 + * allocated and started. For example, a thread that computes primes
1.79 + * larger than a stated value could be written as follows:
1.80 + * <p><hr><blockquote><pre>
1.81 + * class PrimeThread extends Thread {
1.82 + * long minPrime;
1.83 + * PrimeThread(long minPrime) {
1.84 + * this.minPrime = minPrime;
1.85 + * }
1.86 + *
1.87 + * public void run() {
1.88 + * // compute primes larger than minPrime
1.89 + * . . .
1.90 + * }
1.91 + * }
1.92 + * </pre></blockquote><hr>
1.93 + * <p>
1.94 + * The following code would then create a thread and start it running:
1.95 + * <p><blockquote><pre>
1.96 + * PrimeThread p = new PrimeThread(143);
1.97 + * p.start();
1.98 + * </pre></blockquote>
1.99 + * <p>
1.100 + * The other way to create a thread is to declare a class that
1.101 + * implements the <code>Runnable</code> interface. That class then
1.102 + * implements the <code>run</code> method. An instance of the class can
1.103 + * then be allocated, passed as an argument when creating
1.104 + * <code>Thread</code>, and started. The same example in this other
1.105 + * style looks like the following:
1.106 + * <p><hr><blockquote><pre>
1.107 + * class PrimeRun implements Runnable {
1.108 + * long minPrime;
1.109 + * PrimeRun(long minPrime) {
1.110 + * this.minPrime = minPrime;
1.111 + * }
1.112 + *
1.113 + * public void run() {
1.114 + * // compute primes larger than minPrime
1.115 + * . . .
1.116 + * }
1.117 + * }
1.118 + * </pre></blockquote><hr>
1.119 + * <p>
1.120 + * The following code would then create a thread and start it running:
1.121 + * <p><blockquote><pre>
1.122 + * PrimeRun p = new PrimeRun(143);
1.123 + * new Thread(p).start();
1.124 + * </pre></blockquote>
1.125 + * <p>
1.126 + * Every thread has a name for identification purposes. More than
1.127 + * one thread may have the same name. If a name is not specified when
1.128 + * a thread is created, a new name is generated for it.
1.129 + * <p>
1.130 + * Unless otherwise noted, passing a {@code null} argument to a constructor
1.131 + * or method in this class will cause a {@link NullPointerException} to be
1.132 + * thrown.
1.133 + *
1.134 + * @author unascribed
1.135 + * @see Runnable
1.136 + * @see Runtime#exit(int)
1.137 + * @see #run()
1.138 + * @see #stop()
1.139 + * @since JDK1.0
1.140 + */
1.141 +public
1.142 +class Thread implements Runnable {
1.143 + /* Make sure registerNatives is the first thing <clinit> does. */
1.144 + private static native void registerNatives();
1.145 + static {
1.146 + registerNatives();
1.147 + }
1.148 +
1.149 + private char name[];
1.150 + private int priority;
1.151 + private Thread threadQ;
1.152 + private long eetop;
1.153 +
1.154 + /* Whether or not to single_step this thread. */
1.155 + private boolean single_step;
1.156 +
1.157 + /* Whether or not the thread is a daemon thread. */
1.158 + private boolean daemon = false;
1.159 +
1.160 + /* JVM state */
1.161 + private boolean stillborn = false;
1.162 +
1.163 + /* What will be run. */
1.164 + private Runnable target;
1.165 +
1.166 + /* The group of this thread */
1.167 + private ThreadGroup group;
1.168 +
1.169 + /* The context ClassLoader for this thread */
1.170 + private ClassLoader contextClassLoader;
1.171 +
1.172 + /* The inherited AccessControlContext of this thread */
1.173 + private AccessControlContext inheritedAccessControlContext;
1.174 +
1.175 + /* For autonumbering anonymous threads. */
1.176 + private static int threadInitNumber;
1.177 + private static synchronized int nextThreadNum() {
1.178 + return threadInitNumber++;
1.179 + }
1.180 +
1.181 + /* ThreadLocal values pertaining to this thread. This map is maintained
1.182 + * by the ThreadLocal class. */
1.183 + ThreadLocal.ThreadLocalMap threadLocals = null;
1.184 +
1.185 + /*
1.186 + * InheritableThreadLocal values pertaining to this thread. This map is
1.187 + * maintained by the InheritableThreadLocal class.
1.188 + */
1.189 + ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
1.190 +
1.191 + /*
1.192 + * The requested stack size for this thread, or 0 if the creator did
1.193 + * not specify a stack size. It is up to the VM to do whatever it
1.194 + * likes with this number; some VMs will ignore it.
1.195 + */
1.196 + private long stackSize;
1.197 +
1.198 + /*
1.199 + * JVM-private state that persists after native thread termination.
1.200 + */
1.201 + private long nativeParkEventPointer;
1.202 +
1.203 + /*
1.204 + * Thread ID
1.205 + */
1.206 + private long tid;
1.207 +
1.208 + /* For generating thread ID */
1.209 + private static long threadSeqNumber;
1.210 +
1.211 + /* Java thread status for tools,
1.212 + * initialized to indicate thread 'not yet started'
1.213 + */
1.214 +
1.215 + private volatile int threadStatus = 0;
1.216 +
1.217 +
1.218 + private static synchronized long nextThreadID() {
1.219 + return ++threadSeqNumber;
1.220 + }
1.221 +
1.222 + /**
1.223 + * The argument supplied to the current call to
1.224 + * java.util.concurrent.locks.LockSupport.park.
1.225 + * Set by (private) java.util.concurrent.locks.LockSupport.setBlocker
1.226 + * Accessed using java.util.concurrent.locks.LockSupport.getBlocker
1.227 + */
1.228 + volatile Object parkBlocker;
1.229 +
1.230 + /* The object in which this thread is blocked in an interruptible I/O
1.231 + * operation, if any. The blocker's interrupt method should be invoked
1.232 + * after setting this thread's interrupt status.
1.233 + */
1.234 + private volatile Interruptible blocker;
1.235 + private final Object blockerLock = new Object();
1.236 +
1.237 + /* Set the blocker field; invoked via sun.misc.SharedSecrets from java.nio code
1.238 + */
1.239 + void blockedOn(Interruptible b) {
1.240 + synchronized (blockerLock) {
1.241 + blocker = b;
1.242 + }
1.243 + }
1.244 +
1.245 + /**
1.246 + * The minimum priority that a thread can have.
1.247 + */
1.248 + public final static int MIN_PRIORITY = 1;
1.249 +
1.250 + /**
1.251 + * The default priority that is assigned to a thread.
1.252 + */
1.253 + public final static int NORM_PRIORITY = 5;
1.254 +
1.255 + /**
1.256 + * The maximum priority that a thread can have.
1.257 + */
1.258 + public final static int MAX_PRIORITY = 10;
1.259 +
1.260 + /**
1.261 + * Returns a reference to the currently executing thread object.
1.262 + *
1.263 + * @return the currently executing thread.
1.264 + */
1.265 + public static native Thread currentThread();
1.266 +
1.267 + /**
1.268 + * A hint to the scheduler that the current thread is willing to yield
1.269 + * its current use of a processor. The scheduler is free to ignore this
1.270 + * hint.
1.271 + *
1.272 + * <p> Yield is a heuristic attempt to improve relative progression
1.273 + * between threads that would otherwise over-utilise a CPU. Its use
1.274 + * should be combined with detailed profiling and benchmarking to
1.275 + * ensure that it actually has the desired effect.
1.276 + *
1.277 + * <p> It is rarely appropriate to use this method. It may be useful
1.278 + * for debugging or testing purposes, where it may help to reproduce
1.279 + * bugs due to race conditions. It may also be useful when designing
1.280 + * concurrency control constructs such as the ones in the
1.281 + * {@link java.util.concurrent.locks} package.
1.282 + */
1.283 + public static native void yield();
1.284 +
1.285 + /**
1.286 + * Causes the currently executing thread to sleep (temporarily cease
1.287 + * execution) for the specified number of milliseconds, subject to
1.288 + * the precision and accuracy of system timers and schedulers. The thread
1.289 + * does not lose ownership of any monitors.
1.290 + *
1.291 + * @param millis
1.292 + * the length of time to sleep in milliseconds
1.293 + *
1.294 + * @throws IllegalArgumentException
1.295 + * if the value of {@code millis} is negative
1.296 + *
1.297 + * @throws InterruptedException
1.298 + * if any thread has interrupted the current thread. The
1.299 + * <i>interrupted status</i> of the current thread is
1.300 + * cleared when this exception is thrown.
1.301 + */
1.302 + public static native void sleep(long millis) throws InterruptedException;
1.303 +
1.304 + /**
1.305 + * Causes the currently executing thread to sleep (temporarily cease
1.306 + * execution) for the specified number of milliseconds plus the specified
1.307 + * number of nanoseconds, subject to the precision and accuracy of system
1.308 + * timers and schedulers. The thread does not lose ownership of any
1.309 + * monitors.
1.310 + *
1.311 + * @param millis
1.312 + * the length of time to sleep in milliseconds
1.313 + *
1.314 + * @param nanos
1.315 + * {@code 0-999999} additional nanoseconds to sleep
1.316 + *
1.317 + * @throws IllegalArgumentException
1.318 + * if the value of {@code millis} is negative, or the value of
1.319 + * {@code nanos} is not in the range {@code 0-999999}
1.320 + *
1.321 + * @throws InterruptedException
1.322 + * if any thread has interrupted the current thread. The
1.323 + * <i>interrupted status</i> of the current thread is
1.324 + * cleared when this exception is thrown.
1.325 + */
1.326 + public static void sleep(long millis, int nanos)
1.327 + throws InterruptedException {
1.328 + if (millis < 0) {
1.329 + throw new IllegalArgumentException("timeout value is negative");
1.330 + }
1.331 +
1.332 + if (nanos < 0 || nanos > 999999) {
1.333 + throw new IllegalArgumentException(
1.334 + "nanosecond timeout value out of range");
1.335 + }
1.336 +
1.337 + if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
1.338 + millis++;
1.339 + }
1.340 +
1.341 + sleep(millis);
1.342 + }
1.343 +
1.344 + /**
1.345 + * Initializes a Thread.
1.346 + *
1.347 + * @param g the Thread group
1.348 + * @param target the object whose run() method gets called
1.349 + * @param name the name of the new Thread
1.350 + * @param stackSize the desired stack size for the new thread, or
1.351 + * zero to indicate that this parameter is to be ignored.
1.352 + */
1.353 + private void init(ThreadGroup g, Runnable target, String name,
1.354 + long stackSize) {
1.355 + if (name == null) {
1.356 + throw new NullPointerException("name cannot be null");
1.357 + }
1.358 +
1.359 + Thread parent = currentThread();
1.360 + SecurityManager security = System.getSecurityManager();
1.361 + if (g == null) {
1.362 + /* Determine if it's an applet or not */
1.363 +
1.364 + /* If there is a security manager, ask the security manager
1.365 + what to do. */
1.366 + if (security != null) {
1.367 + g = security.getThreadGroup();
1.368 + }
1.369 +
1.370 + /* If the security doesn't have a strong opinion of the matter
1.371 + use the parent thread group. */
1.372 + if (g == null) {
1.373 + g = parent.getThreadGroup();
1.374 + }
1.375 + }
1.376 +
1.377 + /* checkAccess regardless of whether or not threadgroup is
1.378 + explicitly passed in. */
1.379 + g.checkAccess();
1.380 +
1.381 + /*
1.382 + * Do we have the required permissions?
1.383 + */
1.384 + if (security != null) {
1.385 + if (isCCLOverridden(getClass())) {
1.386 + security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
1.387 + }
1.388 + }
1.389 +
1.390 + g.addUnstarted();
1.391 +
1.392 + this.group = g;
1.393 + this.daemon = parent.isDaemon();
1.394 + this.priority = parent.getPriority();
1.395 + this.name = name.toCharArray();
1.396 + if (security == null || isCCLOverridden(parent.getClass()))
1.397 + this.contextClassLoader = parent.getContextClassLoader();
1.398 + else
1.399 + this.contextClassLoader = parent.contextClassLoader;
1.400 + this.inheritedAccessControlContext = AccessController.getContext();
1.401 + this.target = target;
1.402 + setPriority(priority);
1.403 + if (parent.inheritableThreadLocals != null)
1.404 + this.inheritableThreadLocals =
1.405 + ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
1.406 + /* Stash the specified stack size in case the VM cares */
1.407 + this.stackSize = stackSize;
1.408 +
1.409 + /* Set thread ID */
1.410 + tid = nextThreadID();
1.411 + }
1.412 +
1.413 + /**
1.414 + * Throws CloneNotSupportedException as a Thread can not be meaningfully
1.415 + * cloned. Construct a new Thread instead.
1.416 + *
1.417 + * @throws CloneNotSupportedException
1.418 + * always
1.419 + */
1.420 + @Override
1.421 + protected Object clone() throws CloneNotSupportedException {
1.422 + throw new CloneNotSupportedException();
1.423 + }
1.424 +
1.425 + /**
1.426 + * Allocates a new {@code Thread} object. This constructor has the same
1.427 + * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
1.428 + * {@code (null, null, gname)}, where {@code gname} is a newly generated
1.429 + * name. Automatically generated names are of the form
1.430 + * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
1.431 + */
1.432 + public Thread() {
1.433 + init(null, null, "Thread-" + nextThreadNum(), 0);
1.434 + }
1.435 +
1.436 + /**
1.437 + * Allocates a new {@code Thread} object. This constructor has the same
1.438 + * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
1.439 + * {@code (null, target, gname)}, where {@code gname} is a newly generated
1.440 + * name. Automatically generated names are of the form
1.441 + * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
1.442 + *
1.443 + * @param target
1.444 + * the object whose {@code run} method is invoked when this thread
1.445 + * is started. If {@code null}, this classes {@code run} method does
1.446 + * nothing.
1.447 + */
1.448 + public Thread(Runnable target) {
1.449 + init(null, target, "Thread-" + nextThreadNum(), 0);
1.450 + }
1.451 +
1.452 + /**
1.453 + * Allocates a new {@code Thread} object. This constructor has the same
1.454 + * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
1.455 + * {@code (group, target, gname)} ,where {@code gname} is a newly generated
1.456 + * name. Automatically generated names are of the form
1.457 + * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
1.458 + *
1.459 + * @param group
1.460 + * the thread group. If {@code null} and there is a security
1.461 + * manager, the group is determined by {@linkplain
1.462 + * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
1.463 + * If there is not a security manager or {@code
1.464 + * SecurityManager.getThreadGroup()} returns {@code null}, the group
1.465 + * is set to the current thread's thread group.
1.466 + *
1.467 + * @param target
1.468 + * the object whose {@code run} method is invoked when this thread
1.469 + * is started. If {@code null}, this thread's run method is invoked.
1.470 + *
1.471 + * @throws SecurityException
1.472 + * if the current thread cannot create a thread in the specified
1.473 + * thread group
1.474 + */
1.475 + public Thread(ThreadGroup group, Runnable target) {
1.476 + init(group, target, "Thread-" + nextThreadNum(), 0);
1.477 + }
1.478 +
1.479 + /**
1.480 + * Allocates a new {@code Thread} object. This constructor has the same
1.481 + * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
1.482 + * {@code (null, null, name)}.
1.483 + *
1.484 + * @param name
1.485 + * the name of the new thread
1.486 + */
1.487 + public Thread(String name) {
1.488 + init(null, null, name, 0);
1.489 + }
1.490 +
1.491 + /**
1.492 + * Allocates a new {@code Thread} object. This constructor has the same
1.493 + * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
1.494 + * {@code (group, null, name)}.
1.495 + *
1.496 + * @param group
1.497 + * the thread group. If {@code null} and there is a security
1.498 + * manager, the group is determined by {@linkplain
1.499 + * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
1.500 + * If there is not a security manager or {@code
1.501 + * SecurityManager.getThreadGroup()} returns {@code null}, the group
1.502 + * is set to the current thread's thread group.
1.503 + *
1.504 + * @param name
1.505 + * the name of the new thread
1.506 + *
1.507 + * @throws SecurityException
1.508 + * if the current thread cannot create a thread in the specified
1.509 + * thread group
1.510 + */
1.511 + public Thread(ThreadGroup group, String name) {
1.512 + init(group, null, name, 0);
1.513 + }
1.514 +
1.515 + /**
1.516 + * Allocates a new {@code Thread} object. This constructor has the same
1.517 + * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
1.518 + * {@code (null, target, name)}.
1.519 + *
1.520 + * @param target
1.521 + * the object whose {@code run} method is invoked when this thread
1.522 + * is started. If {@code null}, this thread's run method is invoked.
1.523 + *
1.524 + * @param name
1.525 + * the name of the new thread
1.526 + */
1.527 + public Thread(Runnable target, String name) {
1.528 + init(null, target, name, 0);
1.529 + }
1.530 +
1.531 + /**
1.532 + * Allocates a new {@code Thread} object so that it has {@code target}
1.533 + * as its run object, has the specified {@code name} as its name,
1.534 + * and belongs to the thread group referred to by {@code group}.
1.535 + *
1.536 + * <p>If there is a security manager, its
1.537 + * {@link SecurityManager#checkAccess(ThreadGroup) checkAccess}
1.538 + * method is invoked with the ThreadGroup as its argument.
1.539 + *
1.540 + * <p>In addition, its {@code checkPermission} method is invoked with
1.541 + * the {@code RuntimePermission("enableContextClassLoaderOverride")}
1.542 + * permission when invoked directly or indirectly by the constructor
1.543 + * of a subclass which overrides the {@code getContextClassLoader}
1.544 + * or {@code setContextClassLoader} methods.
1.545 + *
1.546 + * <p>The priority of the newly created thread is set equal to the
1.547 + * priority of the thread creating it, that is, the currently running
1.548 + * thread. The method {@linkplain #setPriority setPriority} may be
1.549 + * used to change the priority to a new value.
1.550 + *
1.551 + * <p>The newly created thread is initially marked as being a daemon
1.552 + * thread if and only if the thread creating it is currently marked
1.553 + * as a daemon thread. The method {@linkplain #setDaemon setDaemon}
1.554 + * may be used to change whether or not a thread is a daemon.
1.555 + *
1.556 + * @param group
1.557 + * the thread group. If {@code null} and there is a security
1.558 + * manager, the group is determined by {@linkplain
1.559 + * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
1.560 + * If there is not a security manager or {@code
1.561 + * SecurityManager.getThreadGroup()} returns {@code null}, the group
1.562 + * is set to the current thread's thread group.
1.563 + *
1.564 + * @param target
1.565 + * the object whose {@code run} method is invoked when this thread
1.566 + * is started. If {@code null}, this thread's run method is invoked.
1.567 + *
1.568 + * @param name
1.569 + * the name of the new thread
1.570 + *
1.571 + * @throws SecurityException
1.572 + * if the current thread cannot create a thread in the specified
1.573 + * thread group or cannot override the context class loader methods.
1.574 + */
1.575 + public Thread(ThreadGroup group, Runnable target, String name) {
1.576 + init(group, target, name, 0);
1.577 + }
1.578 +
1.579 + /**
1.580 + * Allocates a new {@code Thread} object so that it has {@code target}
1.581 + * as its run object, has the specified {@code name} as its name,
1.582 + * and belongs to the thread group referred to by {@code group}, and has
1.583 + * the specified <i>stack size</i>.
1.584 + *
1.585 + * <p>This constructor is identical to {@link
1.586 + * #Thread(ThreadGroup,Runnable,String)} with the exception of the fact
1.587 + * that it allows the thread stack size to be specified. The stack size
1.588 + * is the approximate number of bytes of address space that the virtual
1.589 + * machine is to allocate for this thread's stack. <b>The effect of the
1.590 + * {@code stackSize} parameter, if any, is highly platform dependent.</b>
1.591 + *
1.592 + * <p>On some platforms, specifying a higher value for the
1.593 + * {@code stackSize} parameter may allow a thread to achieve greater
1.594 + * recursion depth before throwing a {@link StackOverflowError}.
1.595 + * Similarly, specifying a lower value may allow a greater number of
1.596 + * threads to exist concurrently without throwing an {@link
1.597 + * OutOfMemoryError} (or other internal error). The details of
1.598 + * the relationship between the value of the <tt>stackSize</tt> parameter
1.599 + * and the maximum recursion depth and concurrency level are
1.600 + * platform-dependent. <b>On some platforms, the value of the
1.601 + * {@code stackSize} parameter may have no effect whatsoever.</b>
1.602 + *
1.603 + * <p>The virtual machine is free to treat the {@code stackSize}
1.604 + * parameter as a suggestion. If the specified value is unreasonably low
1.605 + * for the platform, the virtual machine may instead use some
1.606 + * platform-specific minimum value; if the specified value is unreasonably
1.607 + * high, the virtual machine may instead use some platform-specific
1.608 + * maximum. Likewise, the virtual machine is free to round the specified
1.609 + * value up or down as it sees fit (or to ignore it completely).
1.610 + *
1.611 + * <p>Specifying a value of zero for the {@code stackSize} parameter will
1.612 + * cause this constructor to behave exactly like the
1.613 + * {@code Thread(ThreadGroup, Runnable, String)} constructor.
1.614 + *
1.615 + * <p><i>Due to the platform-dependent nature of the behavior of this
1.616 + * constructor, extreme care should be exercised in its use.
1.617 + * The thread stack size necessary to perform a given computation will
1.618 + * likely vary from one JRE implementation to another. In light of this
1.619 + * variation, careful tuning of the stack size parameter may be required,
1.620 + * and the tuning may need to be repeated for each JRE implementation on
1.621 + * which an application is to run.</i>
1.622 + *
1.623 + * <p>Implementation note: Java platform implementers are encouraged to
1.624 + * document their implementation's behavior with respect to the
1.625 + * {@code stackSize} parameter.
1.626 + *
1.627 + *
1.628 + * @param group
1.629 + * the thread group. If {@code null} and there is a security
1.630 + * manager, the group is determined by {@linkplain
1.631 + * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
1.632 + * If there is not a security manager or {@code
1.633 + * SecurityManager.getThreadGroup()} returns {@code null}, the group
1.634 + * is set to the current thread's thread group.
1.635 + *
1.636 + * @param target
1.637 + * the object whose {@code run} method is invoked when this thread
1.638 + * is started. If {@code null}, this thread's run method is invoked.
1.639 + *
1.640 + * @param name
1.641 + * the name of the new thread
1.642 + *
1.643 + * @param stackSize
1.644 + * the desired stack size for the new thread, or zero to indicate
1.645 + * that this parameter is to be ignored.
1.646 + *
1.647 + * @throws SecurityException
1.648 + * if the current thread cannot create a thread in the specified
1.649 + * thread group
1.650 + *
1.651 + * @since 1.4
1.652 + */
1.653 + public Thread(ThreadGroup group, Runnable target, String name,
1.654 + long stackSize) {
1.655 + init(group, target, name, stackSize);
1.656 + }
1.657 +
1.658 + /**
1.659 + * Causes this thread to begin execution; the Java Virtual Machine
1.660 + * calls the <code>run</code> method of this thread.
1.661 + * <p>
1.662 + * The result is that two threads are running concurrently: the
1.663 + * current thread (which returns from the call to the
1.664 + * <code>start</code> method) and the other thread (which executes its
1.665 + * <code>run</code> method).
1.666 + * <p>
1.667 + * It is never legal to start a thread more than once.
1.668 + * In particular, a thread may not be restarted once it has completed
1.669 + * execution.
1.670 + *
1.671 + * @exception IllegalThreadStateException if the thread was already
1.672 + * started.
1.673 + * @see #run()
1.674 + * @see #stop()
1.675 + */
1.676 + public synchronized void start() {
1.677 + /**
1.678 + * This method is not invoked for the main method thread or "system"
1.679 + * group threads created/set up by the VM. Any new functionality added
1.680 + * to this method in the future may have to also be added to the VM.
1.681 + *
1.682 + * A zero status value corresponds to state "NEW".
1.683 + */
1.684 + if (threadStatus != 0)
1.685 + throw new IllegalThreadStateException();
1.686 +
1.687 + /* Notify the group that this thread is about to be started
1.688 + * so that it can be added to the group's list of threads
1.689 + * and the group's unstarted count can be decremented. */
1.690 + group.add(this);
1.691 +
1.692 + boolean started = false;
1.693 + try {
1.694 + start0();
1.695 + started = true;
1.696 + } finally {
1.697 + try {
1.698 + if (!started) {
1.699 + group.threadStartFailed(this);
1.700 + }
1.701 + } catch (Throwable ignore) {
1.702 + /* do nothing. If start0 threw a Throwable then
1.703 + it will be passed up the call stack */
1.704 + }
1.705 + }
1.706 + }
1.707 +
1.708 + private native void start0();
1.709 +
1.710 + /**
1.711 + * If this thread was constructed using a separate
1.712 + * <code>Runnable</code> run object, then that
1.713 + * <code>Runnable</code> object's <code>run</code> method is called;
1.714 + * otherwise, this method does nothing and returns.
1.715 + * <p>
1.716 + * Subclasses of <code>Thread</code> should override this method.
1.717 + *
1.718 + * @see #start()
1.719 + * @see #stop()
1.720 + * @see #Thread(ThreadGroup, Runnable, String)
1.721 + */
1.722 + @Override
1.723 + public void run() {
1.724 + if (target != null) {
1.725 + target.run();
1.726 + }
1.727 + }
1.728 +
1.729 + /**
1.730 + * This method is called by the system to give a Thread
1.731 + * a chance to clean up before it actually exits.
1.732 + */
1.733 + private void exit() {
1.734 + if (group != null) {
1.735 + group.threadTerminated(this);
1.736 + group = null;
1.737 + }
1.738 + /* Aggressively null out all reference fields: see bug 4006245 */
1.739 + target = null;
1.740 + /* Speed the release of some of these resources */
1.741 + threadLocals = null;
1.742 + inheritableThreadLocals = null;
1.743 + inheritedAccessControlContext = null;
1.744 + blocker = null;
1.745 + uncaughtExceptionHandler = null;
1.746 + }
1.747 +
1.748 + /**
1.749 + * Forces the thread to stop executing.
1.750 + * <p>
1.751 + * If there is a security manager installed, its <code>checkAccess</code>
1.752 + * method is called with <code>this</code>
1.753 + * as its argument. This may result in a
1.754 + * <code>SecurityException</code> being raised (in the current thread).
1.755 + * <p>
1.756 + * If this thread is different from the current thread (that is, the current
1.757 + * thread is trying to stop a thread other than itself), the
1.758 + * security manager's <code>checkPermission</code> method (with a
1.759 + * <code>RuntimePermission("stopThread")</code> argument) is called in
1.760 + * addition.
1.761 + * Again, this may result in throwing a
1.762 + * <code>SecurityException</code> (in the current thread).
1.763 + * <p>
1.764 + * The thread represented by this thread is forced to stop whatever
1.765 + * it is doing abnormally and to throw a newly created
1.766 + * <code>ThreadDeath</code> object as an exception.
1.767 + * <p>
1.768 + * It is permitted to stop a thread that has not yet been started.
1.769 + * If the thread is eventually started, it immediately terminates.
1.770 + * <p>
1.771 + * An application should not normally try to catch
1.772 + * <code>ThreadDeath</code> unless it must do some extraordinary
1.773 + * cleanup operation (note that the throwing of
1.774 + * <code>ThreadDeath</code> causes <code>finally</code> clauses of
1.775 + * <code>try</code> statements to be executed before the thread
1.776 + * officially dies). If a <code>catch</code> clause catches a
1.777 + * <code>ThreadDeath</code> object, it is important to rethrow the
1.778 + * object so that the thread actually dies.
1.779 + * <p>
1.780 + * The top-level error handler that reacts to otherwise uncaught
1.781 + * exceptions does not print out a message or otherwise notify the
1.782 + * application if the uncaught exception is an instance of
1.783 + * <code>ThreadDeath</code>.
1.784 + *
1.785 + * @exception SecurityException if the current thread cannot
1.786 + * modify this thread.
1.787 + * @see #interrupt()
1.788 + * @see #checkAccess()
1.789 + * @see #run()
1.790 + * @see #start()
1.791 + * @see ThreadDeath
1.792 + * @see ThreadGroup#uncaughtException(Thread,Throwable)
1.793 + * @see SecurityManager#checkAccess(Thread)
1.794 + * @see SecurityManager#checkPermission
1.795 + * @deprecated This method is inherently unsafe. Stopping a thread with
1.796 + * Thread.stop causes it to unlock all of the monitors that it
1.797 + * has locked (as a natural consequence of the unchecked
1.798 + * <code>ThreadDeath</code> exception propagating up the stack). If
1.799 + * any of the objects previously protected by these monitors were in
1.800 + * an inconsistent state, the damaged objects become visible to
1.801 + * other threads, potentially resulting in arbitrary behavior. Many
1.802 + * uses of <code>stop</code> should be replaced by code that simply
1.803 + * modifies some variable to indicate that the target thread should
1.804 + * stop running. The target thread should check this variable
1.805 + * regularly, and return from its run method in an orderly fashion
1.806 + * if the variable indicates that it is to stop running. If the
1.807 + * target thread waits for long periods (on a condition variable,
1.808 + * for example), the <code>interrupt</code> method should be used to
1.809 + * interrupt the wait.
1.810 + * For more information, see
1.811 + * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
1.812 + * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1.813 + */
1.814 + @Deprecated
1.815 + public final void stop() {
1.816 + stop(new ThreadDeath());
1.817 + }
1.818 +
1.819 + /**
1.820 + * Forces the thread to stop executing.
1.821 + * <p>
1.822 + * If there is a security manager installed, the <code>checkAccess</code>
1.823 + * method of this thread is called, which may result in a
1.824 + * <code>SecurityException</code> being raised (in the current thread).
1.825 + * <p>
1.826 + * If this thread is different from the current thread (that is, the current
1.827 + * thread is trying to stop a thread other than itself) or
1.828 + * <code>obj</code> is not an instance of <code>ThreadDeath</code>, the
1.829 + * security manager's <code>checkPermission</code> method (with the
1.830 + * <code>RuntimePermission("stopThread")</code> argument) is called in
1.831 + * addition.
1.832 + * Again, this may result in throwing a
1.833 + * <code>SecurityException</code> (in the current thread).
1.834 + * <p>
1.835 + * If the argument <code>obj</code> is null, a
1.836 + * <code>NullPointerException</code> is thrown (in the current thread).
1.837 + * <p>
1.838 + * The thread represented by this thread is forced to stop
1.839 + * whatever it is doing abnormally and to throw the
1.840 + * <code>Throwable</code> object <code>obj</code> as an exception. This
1.841 + * is an unusual action to take; normally, the <code>stop</code> method
1.842 + * that takes no arguments should be used.
1.843 + * <p>
1.844 + * It is permitted to stop a thread that has not yet been started.
1.845 + * If the thread is eventually started, it immediately terminates.
1.846 + *
1.847 + * @param obj the Throwable object to be thrown.
1.848 + * @exception SecurityException if the current thread cannot modify
1.849 + * this thread.
1.850 + * @throws NullPointerException if obj is <tt>null</tt>.
1.851 + * @see #interrupt()
1.852 + * @see #checkAccess()
1.853 + * @see #run()
1.854 + * @see #start()
1.855 + * @see #stop()
1.856 + * @see SecurityManager#checkAccess(Thread)
1.857 + * @see SecurityManager#checkPermission
1.858 + * @deprecated This method is inherently unsafe. See {@link #stop()}
1.859 + * for details. An additional danger of this
1.860 + * method is that it may be used to generate exceptions that the
1.861 + * target thread is unprepared to handle (including checked
1.862 + * exceptions that the thread could not possibly throw, were it
1.863 + * not for this method).
1.864 + * For more information, see
1.865 + * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
1.866 + * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1.867 + */
1.868 + @Deprecated
1.869 + public final synchronized void stop(Throwable obj) {
1.870 + if (obj == null)
1.871 + throw new NullPointerException();
1.872 +
1.873 + SecurityManager security = System.getSecurityManager();
1.874 + if (security != null) {
1.875 + checkAccess();
1.876 + if ((this != Thread.currentThread()) ||
1.877 + (!(obj instanceof ThreadDeath))) {
1.878 + security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
1.879 + }
1.880 + }
1.881 + // A zero status value corresponds to "NEW", it can't change to
1.882 + // not-NEW because we hold the lock.
1.883 + if (threadStatus != 0) {
1.884 + resume(); // Wake up thread if it was suspended; no-op otherwise
1.885 + }
1.886 +
1.887 + // The VM can handle all thread states
1.888 + stop0(obj);
1.889 + }
1.890 +
1.891 + /**
1.892 + * Interrupts this thread.
1.893 + *
1.894 + * <p> Unless the current thread is interrupting itself, which is
1.895 + * always permitted, the {@link #checkAccess() checkAccess} method
1.896 + * of this thread is invoked, which may cause a {@link
1.897 + * SecurityException} to be thrown.
1.898 + *
1.899 + * <p> If this thread is blocked in an invocation of the {@link
1.900 + * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
1.901 + * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
1.902 + * class, or of the {@link #join()}, {@link #join(long)}, {@link
1.903 + * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
1.904 + * methods of this class, then its interrupt status will be cleared and it
1.905 + * will receive an {@link InterruptedException}.
1.906 + *
1.907 + * <p> If this thread is blocked in an I/O operation upon an {@link
1.908 + * java.nio.channels.InterruptibleChannel </code>interruptible
1.909 + * channel<code>} then the channel will be closed, the thread's interrupt
1.910 + * status will be set, and the thread will receive a {@link
1.911 + * java.nio.channels.ClosedByInterruptException}.
1.912 + *
1.913 + * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
1.914 + * then the thread's interrupt status will be set and it will return
1.915 + * immediately from the selection operation, possibly with a non-zero
1.916 + * value, just as if the selector's {@link
1.917 + * java.nio.channels.Selector#wakeup wakeup} method were invoked.
1.918 + *
1.919 + * <p> If none of the previous conditions hold then this thread's interrupt
1.920 + * status will be set. </p>
1.921 + *
1.922 + * <p> Interrupting a thread that is not alive need not have any effect.
1.923 + *
1.924 + * @throws SecurityException
1.925 + * if the current thread cannot modify this thread
1.926 + *
1.927 + * @revised 6.0
1.928 + * @spec JSR-51
1.929 + */
1.930 + public void interrupt() {
1.931 + if (this != Thread.currentThread())
1.932 + checkAccess();
1.933 +
1.934 + synchronized (blockerLock) {
1.935 + Interruptible b = blocker;
1.936 + if (b != null) {
1.937 + interrupt0(); // Just to set the interrupt flag
1.938 + b.interrupt(this);
1.939 + return;
1.940 + }
1.941 + }
1.942 + interrupt0();
1.943 + }
1.944 +
1.945 + /**
1.946 + * Tests whether the current thread has been interrupted. The
1.947 + * <i>interrupted status</i> of the thread is cleared by this method. In
1.948 + * other words, if this method were to be called twice in succession, the
1.949 + * second call would return false (unless the current thread were
1.950 + * interrupted again, after the first call had cleared its interrupted
1.951 + * status and before the second call had examined it).
1.952 + *
1.953 + * <p>A thread interruption ignored because a thread was not alive
1.954 + * at the time of the interrupt will be reflected by this method
1.955 + * returning false.
1.956 + *
1.957 + * @return <code>true</code> if the current thread has been interrupted;
1.958 + * <code>false</code> otherwise.
1.959 + * @see #isInterrupted()
1.960 + * @revised 6.0
1.961 + */
1.962 + public static boolean interrupted() {
1.963 + return currentThread().isInterrupted(true);
1.964 + }
1.965 +
1.966 + /**
1.967 + * Tests whether this thread has been interrupted. The <i>interrupted
1.968 + * status</i> of the thread is unaffected by this method.
1.969 + *
1.970 + * <p>A thread interruption ignored because a thread was not alive
1.971 + * at the time of the interrupt will be reflected by this method
1.972 + * returning false.
1.973 + *
1.974 + * @return <code>true</code> if this thread has been interrupted;
1.975 + * <code>false</code> otherwise.
1.976 + * @see #interrupted()
1.977 + * @revised 6.0
1.978 + */
1.979 + public boolean isInterrupted() {
1.980 + return isInterrupted(false);
1.981 + }
1.982 +
1.983 + /**
1.984 + * Tests if some Thread has been interrupted. The interrupted state
1.985 + * is reset or not based on the value of ClearInterrupted that is
1.986 + * passed.
1.987 + */
1.988 + private native boolean isInterrupted(boolean ClearInterrupted);
1.989 +
1.990 + /**
1.991 + * Throws {@link NoSuchMethodError}.
1.992 + *
1.993 + * @deprecated This method was originally designed to destroy this
1.994 + * thread without any cleanup. Any monitors it held would have
1.995 + * remained locked. However, the method was never implemented.
1.996 + * If if were to be implemented, it would be deadlock-prone in
1.997 + * much the manner of {@link #suspend}. If the target thread held
1.998 + * a lock protecting a critical system resource when it was
1.999 + * destroyed, no thread could ever access this resource again.
1.1000 + * If another thread ever attempted to lock this resource, deadlock
1.1001 + * would result. Such deadlocks typically manifest themselves as
1.1002 + * "frozen" processes. For more information, see
1.1003 + * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">
1.1004 + * Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1.1005 + * @throws NoSuchMethodError always
1.1006 + */
1.1007 + @Deprecated
1.1008 + public void destroy() {
1.1009 + throw new NoSuchMethodError();
1.1010 + }
1.1011 +
1.1012 + /**
1.1013 + * Tests if this thread is alive. A thread is alive if it has
1.1014 + * been started and has not yet died.
1.1015 + *
1.1016 + * @return <code>true</code> if this thread is alive;
1.1017 + * <code>false</code> otherwise.
1.1018 + */
1.1019 + public final native boolean isAlive();
1.1020 +
1.1021 + /**
1.1022 + * Suspends this thread.
1.1023 + * <p>
1.1024 + * First, the <code>checkAccess</code> method of this thread is called
1.1025 + * with no arguments. This may result in throwing a
1.1026 + * <code>SecurityException </code>(in the current thread).
1.1027 + * <p>
1.1028 + * If the thread is alive, it is suspended and makes no further
1.1029 + * progress unless and until it is resumed.
1.1030 + *
1.1031 + * @exception SecurityException if the current thread cannot modify
1.1032 + * this thread.
1.1033 + * @see #checkAccess
1.1034 + * @deprecated This method has been deprecated, as it is
1.1035 + * inherently deadlock-prone. If the target thread holds a lock on the
1.1036 + * monitor protecting a critical system resource when it is suspended, no
1.1037 + * thread can access this resource until the target thread is resumed. If
1.1038 + * the thread that would resume the target thread attempts to lock this
1.1039 + * monitor prior to calling <code>resume</code>, deadlock results. Such
1.1040 + * deadlocks typically manifest themselves as "frozen" processes.
1.1041 + * For more information, see
1.1042 + * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
1.1043 + * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1.1044 + */
1.1045 + @Deprecated
1.1046 + public final void suspend() {
1.1047 + checkAccess();
1.1048 + suspend0();
1.1049 + }
1.1050 +
1.1051 + /**
1.1052 + * Resumes a suspended thread.
1.1053 + * <p>
1.1054 + * First, the <code>checkAccess</code> method of this thread is called
1.1055 + * with no arguments. This may result in throwing a
1.1056 + * <code>SecurityException</code> (in the current thread).
1.1057 + * <p>
1.1058 + * If the thread is alive but suspended, it is resumed and is
1.1059 + * permitted to make progress in its execution.
1.1060 + *
1.1061 + * @exception SecurityException if the current thread cannot modify this
1.1062 + * thread.
1.1063 + * @see #checkAccess
1.1064 + * @see #suspend()
1.1065 + * @deprecated This method exists solely for use with {@link #suspend},
1.1066 + * which has been deprecated because it is deadlock-prone.
1.1067 + * For more information, see
1.1068 + * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
1.1069 + * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1.1070 + */
1.1071 + @Deprecated
1.1072 + public final void resume() {
1.1073 + checkAccess();
1.1074 + resume0();
1.1075 + }
1.1076 +
1.1077 + /**
1.1078 + * Changes the priority of this thread.
1.1079 + * <p>
1.1080 + * First the <code>checkAccess</code> method of this thread is called
1.1081 + * with no arguments. This may result in throwing a
1.1082 + * <code>SecurityException</code>.
1.1083 + * <p>
1.1084 + * Otherwise, the priority of this thread is set to the smaller of
1.1085 + * the specified <code>newPriority</code> and the maximum permitted
1.1086 + * priority of the thread's thread group.
1.1087 + *
1.1088 + * @param newPriority priority to set this thread to
1.1089 + * @exception IllegalArgumentException If the priority is not in the
1.1090 + * range <code>MIN_PRIORITY</code> to
1.1091 + * <code>MAX_PRIORITY</code>.
1.1092 + * @exception SecurityException if the current thread cannot modify
1.1093 + * this thread.
1.1094 + * @see #getPriority
1.1095 + * @see #checkAccess()
1.1096 + * @see #getThreadGroup()
1.1097 + * @see #MAX_PRIORITY
1.1098 + * @see #MIN_PRIORITY
1.1099 + * @see ThreadGroup#getMaxPriority()
1.1100 + */
1.1101 + public final void setPriority(int newPriority) {
1.1102 + ThreadGroup g;
1.1103 + checkAccess();
1.1104 + if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
1.1105 + throw new IllegalArgumentException();
1.1106 + }
1.1107 + if((g = getThreadGroup()) != null) {
1.1108 + if (newPriority > g.getMaxPriority()) {
1.1109 + newPriority = g.getMaxPriority();
1.1110 + }
1.1111 + setPriority0(priority = newPriority);
1.1112 + }
1.1113 + }
1.1114 +
1.1115 + /**
1.1116 + * Returns this thread's priority.
1.1117 + *
1.1118 + * @return this thread's priority.
1.1119 + * @see #setPriority
1.1120 + */
1.1121 + public final int getPriority() {
1.1122 + return priority;
1.1123 + }
1.1124 +
1.1125 + /**
1.1126 + * Changes the name of this thread to be equal to the argument
1.1127 + * <code>name</code>.
1.1128 + * <p>
1.1129 + * First the <code>checkAccess</code> method of this thread is called
1.1130 + * with no arguments. This may result in throwing a
1.1131 + * <code>SecurityException</code>.
1.1132 + *
1.1133 + * @param name the new name for this thread.
1.1134 + * @exception SecurityException if the current thread cannot modify this
1.1135 + * thread.
1.1136 + * @see #getName
1.1137 + * @see #checkAccess()
1.1138 + */
1.1139 + public final void setName(String name) {
1.1140 + checkAccess();
1.1141 + this.name = name.toCharArray();
1.1142 + }
1.1143 +
1.1144 + /**
1.1145 + * Returns this thread's name.
1.1146 + *
1.1147 + * @return this thread's name.
1.1148 + * @see #setName(String)
1.1149 + */
1.1150 + public final String getName() {
1.1151 + return String.valueOf(name);
1.1152 + }
1.1153 +
1.1154 + /**
1.1155 + * Returns the thread group to which this thread belongs.
1.1156 + * This method returns null if this thread has died
1.1157 + * (been stopped).
1.1158 + *
1.1159 + * @return this thread's thread group.
1.1160 + */
1.1161 + public final ThreadGroup getThreadGroup() {
1.1162 + return group;
1.1163 + }
1.1164 +
1.1165 + /**
1.1166 + * Returns an estimate of the number of active threads in the current
1.1167 + * thread's {@linkplain java.lang.ThreadGroup thread group} and its
1.1168 + * subgroups. Recursively iterates over all subgroups in the current
1.1169 + * thread's thread group.
1.1170 + *
1.1171 + * <p> The value returned is only an estimate because the number of
1.1172 + * threads may change dynamically while this method traverses internal
1.1173 + * data structures, and might be affected by the presence of certain
1.1174 + * system threads. This method is intended primarily for debugging
1.1175 + * and monitoring purposes.
1.1176 + *
1.1177 + * @return an estimate of the number of active threads in the current
1.1178 + * thread's thread group and in any other thread group that
1.1179 + * has the current thread's thread group as an ancestor
1.1180 + */
1.1181 + public static int activeCount() {
1.1182 + return currentThread().getThreadGroup().activeCount();
1.1183 + }
1.1184 +
1.1185 + /**
1.1186 + * Copies into the specified array every active thread in the current
1.1187 + * thread's thread group and its subgroups. This method simply
1.1188 + * invokes the {@link java.lang.ThreadGroup#enumerate(Thread[])}
1.1189 + * method of the current thread's thread group.
1.1190 + *
1.1191 + * <p> An application might use the {@linkplain #activeCount activeCount}
1.1192 + * method to get an estimate of how big the array should be, however
1.1193 + * <i>if the array is too short to hold all the threads, the extra threads
1.1194 + * are silently ignored.</i> If it is critical to obtain every active
1.1195 + * thread in the current thread's thread group and its subgroups, the
1.1196 + * invoker should verify that the returned int value is strictly less
1.1197 + * than the length of {@code tarray}.
1.1198 + *
1.1199 + * <p> Due to the inherent race condition in this method, it is recommended
1.1200 + * that the method only be used for debugging and monitoring purposes.
1.1201 + *
1.1202 + * @param tarray
1.1203 + * an array into which to put the list of threads
1.1204 + *
1.1205 + * @return the number of threads put into the array
1.1206 + *
1.1207 + * @throws SecurityException
1.1208 + * if {@link java.lang.ThreadGroup#checkAccess} determines that
1.1209 + * the current thread cannot access its thread group
1.1210 + */
1.1211 + public static int enumerate(Thread tarray[]) {
1.1212 + return currentThread().getThreadGroup().enumerate(tarray);
1.1213 + }
1.1214 +
1.1215 + /**
1.1216 + * Counts the number of stack frames in this thread. The thread must
1.1217 + * be suspended.
1.1218 + *
1.1219 + * @return the number of stack frames in this thread.
1.1220 + * @exception IllegalThreadStateException if this thread is not
1.1221 + * suspended.
1.1222 + * @deprecated The definition of this call depends on {@link #suspend},
1.1223 + * which is deprecated. Further, the results of this call
1.1224 + * were never well-defined.
1.1225 + */
1.1226 + @Deprecated
1.1227 + public native int countStackFrames();
1.1228 +
1.1229 + /**
1.1230 + * Waits at most {@code millis} milliseconds for this thread to
1.1231 + * die. A timeout of {@code 0} means to wait forever.
1.1232 + *
1.1233 + * <p> This implementation uses a loop of {@code this.wait} calls
1.1234 + * conditioned on {@code this.isAlive}. As a thread terminates the
1.1235 + * {@code this.notifyAll} method is invoked. It is recommended that
1.1236 + * applications not use {@code wait}, {@code notify}, or
1.1237 + * {@code notifyAll} on {@code Thread} instances.
1.1238 + *
1.1239 + * @param millis
1.1240 + * the time to wait in milliseconds
1.1241 + *
1.1242 + * @throws IllegalArgumentException
1.1243 + * if the value of {@code millis} is negative
1.1244 + *
1.1245 + * @throws InterruptedException
1.1246 + * if any thread has interrupted the current thread. The
1.1247 + * <i>interrupted status</i> of the current thread is
1.1248 + * cleared when this exception is thrown.
1.1249 + */
1.1250 + public final synchronized void join(long millis)
1.1251 + throws InterruptedException {
1.1252 + long base = System.currentTimeMillis();
1.1253 + long now = 0;
1.1254 +
1.1255 + if (millis < 0) {
1.1256 + throw new IllegalArgumentException("timeout value is negative");
1.1257 + }
1.1258 +
1.1259 + if (millis == 0) {
1.1260 + while (isAlive()) {
1.1261 + wait(0);
1.1262 + }
1.1263 + } else {
1.1264 + while (isAlive()) {
1.1265 + long delay = millis - now;
1.1266 + if (delay <= 0) {
1.1267 + break;
1.1268 + }
1.1269 + wait(delay);
1.1270 + now = System.currentTimeMillis() - base;
1.1271 + }
1.1272 + }
1.1273 + }
1.1274 +
1.1275 + /**
1.1276 + * Waits at most {@code millis} milliseconds plus
1.1277 + * {@code nanos} nanoseconds for this thread to die.
1.1278 + *
1.1279 + * <p> This implementation uses a loop of {@code this.wait} calls
1.1280 + * conditioned on {@code this.isAlive}. As a thread terminates the
1.1281 + * {@code this.notifyAll} method is invoked. It is recommended that
1.1282 + * applications not use {@code wait}, {@code notify}, or
1.1283 + * {@code notifyAll} on {@code Thread} instances.
1.1284 + *
1.1285 + * @param millis
1.1286 + * the time to wait in milliseconds
1.1287 + *
1.1288 + * @param nanos
1.1289 + * {@code 0-999999} additional nanoseconds to wait
1.1290 + *
1.1291 + * @throws IllegalArgumentException
1.1292 + * if the value of {@code millis} is negative, or the value
1.1293 + * of {@code nanos} is not in the range {@code 0-999999}
1.1294 + *
1.1295 + * @throws InterruptedException
1.1296 + * if any thread has interrupted the current thread. The
1.1297 + * <i>interrupted status</i> of the current thread is
1.1298 + * cleared when this exception is thrown.
1.1299 + */
1.1300 + public final synchronized void join(long millis, int nanos)
1.1301 + throws InterruptedException {
1.1302 +
1.1303 + if (millis < 0) {
1.1304 + throw new IllegalArgumentException("timeout value is negative");
1.1305 + }
1.1306 +
1.1307 + if (nanos < 0 || nanos > 999999) {
1.1308 + throw new IllegalArgumentException(
1.1309 + "nanosecond timeout value out of range");
1.1310 + }
1.1311 +
1.1312 + if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
1.1313 + millis++;
1.1314 + }
1.1315 +
1.1316 + join(millis);
1.1317 + }
1.1318 +
1.1319 + /**
1.1320 + * Waits for this thread to die.
1.1321 + *
1.1322 + * <p> An invocation of this method behaves in exactly the same
1.1323 + * way as the invocation
1.1324 + *
1.1325 + * <blockquote>
1.1326 + * {@linkplain #join(long) join}{@code (0)}
1.1327 + * </blockquote>
1.1328 + *
1.1329 + * @throws InterruptedException
1.1330 + * if any thread has interrupted the current thread. The
1.1331 + * <i>interrupted status</i> of the current thread is
1.1332 + * cleared when this exception is thrown.
1.1333 + */
1.1334 + public final void join() throws InterruptedException {
1.1335 + join(0);
1.1336 + }
1.1337 +
1.1338 + /**
1.1339 + * Prints a stack trace of the current thread to the standard error stream.
1.1340 + * This method is used only for debugging.
1.1341 + *
1.1342 + * @see Throwable#printStackTrace()
1.1343 + */
1.1344 + public static void dumpStack() {
1.1345 + new Exception("Stack trace").printStackTrace();
1.1346 + }
1.1347 +
1.1348 + /**
1.1349 + * Marks this thread as either a {@linkplain #isDaemon daemon} thread
1.1350 + * or a user thread. The Java Virtual Machine exits when the only
1.1351 + * threads running are all daemon threads.
1.1352 + *
1.1353 + * <p> This method must be invoked before the thread is started.
1.1354 + *
1.1355 + * @param on
1.1356 + * if {@code true}, marks this thread as a daemon thread
1.1357 + *
1.1358 + * @throws IllegalThreadStateException
1.1359 + * if this thread is {@linkplain #isAlive alive}
1.1360 + *
1.1361 + * @throws SecurityException
1.1362 + * if {@link #checkAccess} determines that the current
1.1363 + * thread cannot modify this thread
1.1364 + */
1.1365 + public final void setDaemon(boolean on) {
1.1366 + checkAccess();
1.1367 + if (isAlive()) {
1.1368 + throw new IllegalThreadStateException();
1.1369 + }
1.1370 + daemon = on;
1.1371 + }
1.1372 +
1.1373 + /**
1.1374 + * Tests if this thread is a daemon thread.
1.1375 + *
1.1376 + * @return <code>true</code> if this thread is a daemon thread;
1.1377 + * <code>false</code> otherwise.
1.1378 + * @see #setDaemon(boolean)
1.1379 + */
1.1380 + public final boolean isDaemon() {
1.1381 + return daemon;
1.1382 + }
1.1383 +
1.1384 + /**
1.1385 + * Determines if the currently running thread has permission to
1.1386 + * modify this thread.
1.1387 + * <p>
1.1388 + * If there is a security manager, its <code>checkAccess</code> method
1.1389 + * is called with this thread as its argument. This may result in
1.1390 + * throwing a <code>SecurityException</code>.
1.1391 + *
1.1392 + * @exception SecurityException if the current thread is not allowed to
1.1393 + * access this thread.
1.1394 + * @see SecurityManager#checkAccess(Thread)
1.1395 + */
1.1396 + public final void checkAccess() {
1.1397 + SecurityManager security = System.getSecurityManager();
1.1398 + if (security != null) {
1.1399 + security.checkAccess(this);
1.1400 + }
1.1401 + }
1.1402 +
1.1403 + /**
1.1404 + * Returns a string representation of this thread, including the
1.1405 + * thread's name, priority, and thread group.
1.1406 + *
1.1407 + * @return a string representation of this thread.
1.1408 + */
1.1409 + public String toString() {
1.1410 + ThreadGroup group = getThreadGroup();
1.1411 + if (group != null) {
1.1412 + return "Thread[" + getName() + "," + getPriority() + "," +
1.1413 + group.getName() + "]";
1.1414 + } else {
1.1415 + return "Thread[" + getName() + "," + getPriority() + "," +
1.1416 + "" + "]";
1.1417 + }
1.1418 + }
1.1419 +
1.1420 + /**
1.1421 + * Returns the context ClassLoader for this Thread. The context
1.1422 + * ClassLoader is provided by the creator of the thread for use
1.1423 + * by code running in this thread when loading classes and resources.
1.1424 + * If not {@linkplain #setContextClassLoader set}, the default is the
1.1425 + * ClassLoader context of the parent Thread. The context ClassLoader of the
1.1426 + * primordial thread is typically set to the class loader used to load the
1.1427 + * application.
1.1428 + *
1.1429 + * <p>If a security manager is present, and the invoker's class loader is not
1.1430 + * {@code null} and is not the same as or an ancestor of the context class
1.1431 + * loader, then this method invokes the security manager's {@link
1.1432 + * SecurityManager#checkPermission(java.security.Permission) checkPermission}
1.1433 + * method with a {@link RuntimePermission RuntimePermission}{@code
1.1434 + * ("getClassLoader")} permission to verify that retrieval of the context
1.1435 + * class loader is permitted.
1.1436 + *
1.1437 + * @return the context ClassLoader for this Thread, or {@code null}
1.1438 + * indicating the system class loader (or, failing that, the
1.1439 + * bootstrap class loader)
1.1440 + *
1.1441 + * @throws SecurityException
1.1442 + * if the current thread cannot get the context ClassLoader
1.1443 + *
1.1444 + * @since 1.2
1.1445 + */
1.1446 + public ClassLoader getContextClassLoader() {
1.1447 + if (contextClassLoader == null)
1.1448 + return null;
1.1449 + SecurityManager sm = System.getSecurityManager();
1.1450 + if (sm != null) {
1.1451 + ClassLoader ccl = ClassLoader.getCallerClassLoader();
1.1452 + if (ccl != null && ccl != contextClassLoader &&
1.1453 + !contextClassLoader.isAncestor(ccl)) {
1.1454 + sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
1.1455 + }
1.1456 + }
1.1457 + return contextClassLoader;
1.1458 + }
1.1459 +
1.1460 + /**
1.1461 + * Sets the context ClassLoader for this Thread. The context
1.1462 + * ClassLoader can be set when a thread is created, and allows
1.1463 + * the creator of the thread to provide the appropriate class loader,
1.1464 + * through {@code getContextClassLoader}, to code running in the thread
1.1465 + * when loading classes and resources.
1.1466 + *
1.1467 + * <p>If a security manager is present, its {@link
1.1468 + * SecurityManager#checkPermission(java.security.Permission) checkPermission}
1.1469 + * method is invoked with a {@link RuntimePermission RuntimePermission}{@code
1.1470 + * ("setContextClassLoader")} permission to see if setting the context
1.1471 + * ClassLoader is permitted.
1.1472 + *
1.1473 + * @param cl
1.1474 + * the context ClassLoader for this Thread, or null indicating the
1.1475 + * system class loader (or, failing that, the bootstrap class loader)
1.1476 + *
1.1477 + * @throws SecurityException
1.1478 + * if the current thread cannot set the context ClassLoader
1.1479 + *
1.1480 + * @since 1.2
1.1481 + */
1.1482 + public void setContextClassLoader(ClassLoader cl) {
1.1483 + SecurityManager sm = System.getSecurityManager();
1.1484 + if (sm != null) {
1.1485 + sm.checkPermission(new RuntimePermission("setContextClassLoader"));
1.1486 + }
1.1487 + contextClassLoader = cl;
1.1488 + }
1.1489 +
1.1490 + /**
1.1491 + * Returns <tt>true</tt> if and only if the current thread holds the
1.1492 + * monitor lock on the specified object.
1.1493 + *
1.1494 + * <p>This method is designed to allow a program to assert that
1.1495 + * the current thread already holds a specified lock:
1.1496 + * <pre>
1.1497 + * assert Thread.holdsLock(obj);
1.1498 + * </pre>
1.1499 + *
1.1500 + * @param obj the object on which to test lock ownership
1.1501 + * @throws NullPointerException if obj is <tt>null</tt>
1.1502 + * @return <tt>true</tt> if the current thread holds the monitor lock on
1.1503 + * the specified object.
1.1504 + * @since 1.4
1.1505 + */
1.1506 + public static native boolean holdsLock(Object obj);
1.1507 +
1.1508 + private static final StackTraceElement[] EMPTY_STACK_TRACE
1.1509 + = new StackTraceElement[0];
1.1510 +
1.1511 + /**
1.1512 + * Returns an array of stack trace elements representing the stack dump
1.1513 + * of this thread. This method will return a zero-length array if
1.1514 + * this thread has not started, has started but has not yet been
1.1515 + * scheduled to run by the system, or has terminated.
1.1516 + * If the returned array is of non-zero length then the first element of
1.1517 + * the array represents the top of the stack, which is the most recent
1.1518 + * method invocation in the sequence. The last element of the array
1.1519 + * represents the bottom of the stack, which is the least recent method
1.1520 + * invocation in the sequence.
1.1521 + *
1.1522 + * <p>If there is a security manager, and this thread is not
1.1523 + * the current thread, then the security manager's
1.1524 + * <tt>checkPermission</tt> method is called with a
1.1525 + * <tt>RuntimePermission("getStackTrace")</tt> permission
1.1526 + * to see if it's ok to get the stack trace.
1.1527 + *
1.1528 + * <p>Some virtual machines may, under some circumstances, omit one
1.1529 + * or more stack frames from the stack trace. In the extreme case,
1.1530 + * a virtual machine that has no stack trace information concerning
1.1531 + * this thread is permitted to return a zero-length array from this
1.1532 + * method.
1.1533 + *
1.1534 + * @return an array of <tt>StackTraceElement</tt>,
1.1535 + * each represents one stack frame.
1.1536 + *
1.1537 + * @throws SecurityException
1.1538 + * if a security manager exists and its
1.1539 + * <tt>checkPermission</tt> method doesn't allow
1.1540 + * getting the stack trace of thread.
1.1541 + * @see SecurityManager#checkPermission
1.1542 + * @see RuntimePermission
1.1543 + * @see Throwable#getStackTrace
1.1544 + *
1.1545 + * @since 1.5
1.1546 + */
1.1547 + public StackTraceElement[] getStackTrace() {
1.1548 + if (this != Thread.currentThread()) {
1.1549 + // check for getStackTrace permission
1.1550 + SecurityManager security = System.getSecurityManager();
1.1551 + if (security != null) {
1.1552 + security.checkPermission(
1.1553 + SecurityConstants.GET_STACK_TRACE_PERMISSION);
1.1554 + }
1.1555 + // optimization so we do not call into the vm for threads that
1.1556 + // have not yet started or have terminated
1.1557 + if (!isAlive()) {
1.1558 + return EMPTY_STACK_TRACE;
1.1559 + }
1.1560 + StackTraceElement[][] stackTraceArray = dumpThreads(new Thread[] {this});
1.1561 + StackTraceElement[] stackTrace = stackTraceArray[0];
1.1562 + // a thread that was alive during the previous isAlive call may have
1.1563 + // since terminated, therefore not having a stacktrace.
1.1564 + if (stackTrace == null) {
1.1565 + stackTrace = EMPTY_STACK_TRACE;
1.1566 + }
1.1567 + return stackTrace;
1.1568 + } else {
1.1569 + // Don't need JVM help for current thread
1.1570 + return (new Exception()).getStackTrace();
1.1571 + }
1.1572 + }
1.1573 +
1.1574 + /**
1.1575 + * Returns a map of stack traces for all live threads.
1.1576 + * The map keys are threads and each map value is an array of
1.1577 + * <tt>StackTraceElement</tt> that represents the stack dump
1.1578 + * of the corresponding <tt>Thread</tt>.
1.1579 + * The returned stack traces are in the format specified for
1.1580 + * the {@link #getStackTrace getStackTrace} method.
1.1581 + *
1.1582 + * <p>The threads may be executing while this method is called.
1.1583 + * The stack trace of each thread only represents a snapshot and
1.1584 + * each stack trace may be obtained at different time. A zero-length
1.1585 + * array will be returned in the map value if the virtual machine has
1.1586 + * no stack trace information about a thread.
1.1587 + *
1.1588 + * <p>If there is a security manager, then the security manager's
1.1589 + * <tt>checkPermission</tt> method is called with a
1.1590 + * <tt>RuntimePermission("getStackTrace")</tt> permission as well as
1.1591 + * <tt>RuntimePermission("modifyThreadGroup")</tt> permission
1.1592 + * to see if it is ok to get the stack trace of all threads.
1.1593 + *
1.1594 + * @return a <tt>Map</tt> from <tt>Thread</tt> to an array of
1.1595 + * <tt>StackTraceElement</tt> that represents the stack trace of
1.1596 + * the corresponding thread.
1.1597 + *
1.1598 + * @throws SecurityException
1.1599 + * if a security manager exists and its
1.1600 + * <tt>checkPermission</tt> method doesn't allow
1.1601 + * getting the stack trace of thread.
1.1602 + * @see #getStackTrace
1.1603 + * @see SecurityManager#checkPermission
1.1604 + * @see RuntimePermission
1.1605 + * @see Throwable#getStackTrace
1.1606 + *
1.1607 + * @since 1.5
1.1608 + */
1.1609 + public static Map<Thread, StackTraceElement[]> getAllStackTraces() {
1.1610 + // check for getStackTrace permission
1.1611 + SecurityManager security = System.getSecurityManager();
1.1612 + if (security != null) {
1.1613 + security.checkPermission(
1.1614 + SecurityConstants.GET_STACK_TRACE_PERMISSION);
1.1615 + security.checkPermission(
1.1616 + SecurityConstants.MODIFY_THREADGROUP_PERMISSION);
1.1617 + }
1.1618 +
1.1619 + // Get a snapshot of the list of all threads
1.1620 + Thread[] threads = getThreads();
1.1621 + StackTraceElement[][] traces = dumpThreads(threads);
1.1622 + Map<Thread, StackTraceElement[]> m = new HashMap<>(threads.length);
1.1623 + for (int i = 0; i < threads.length; i++) {
1.1624 + StackTraceElement[] stackTrace = traces[i];
1.1625 + if (stackTrace != null) {
1.1626 + m.put(threads[i], stackTrace);
1.1627 + }
1.1628 + // else terminated so we don't put it in the map
1.1629 + }
1.1630 + return m;
1.1631 + }
1.1632 +
1.1633 +
1.1634 + private static final RuntimePermission SUBCLASS_IMPLEMENTATION_PERMISSION =
1.1635 + new RuntimePermission("enableContextClassLoaderOverride");
1.1636 +
1.1637 + /** cache of subclass security audit results */
1.1638 + /* Replace with ConcurrentReferenceHashMap when/if it appears in a future
1.1639 + * release */
1.1640 + private static class Caches {
1.1641 + /** cache of subclass security audit results */
1.1642 + static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits =
1.1643 + new ConcurrentHashMap<>();
1.1644 +
1.1645 + /** queue for WeakReferences to audited subclasses */
1.1646 + static final ReferenceQueue<Class<?>> subclassAuditsQueue =
1.1647 + new ReferenceQueue<>();
1.1648 + }
1.1649 +
1.1650 + /**
1.1651 + * Verifies that this (possibly subclass) instance can be constructed
1.1652 + * without violating security constraints: the subclass must not override
1.1653 + * security-sensitive non-final methods, or else the
1.1654 + * "enableContextClassLoaderOverride" RuntimePermission is checked.
1.1655 + */
1.1656 + private static boolean isCCLOverridden(Class cl) {
1.1657 + if (cl == Thread.class)
1.1658 + return false;
1.1659 +
1.1660 + processQueue(Caches.subclassAuditsQueue, Caches.subclassAudits);
1.1661 + WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue);
1.1662 + Boolean result = Caches.subclassAudits.get(key);
1.1663 + if (result == null) {
1.1664 + result = Boolean.valueOf(auditSubclass(cl));
1.1665 + Caches.subclassAudits.putIfAbsent(key, result);
1.1666 + }
1.1667 +
1.1668 + return result.booleanValue();
1.1669 + }
1.1670 +
1.1671 + /**
1.1672 + * Performs reflective checks on given subclass to verify that it doesn't
1.1673 + * override security-sensitive non-final methods. Returns true if the
1.1674 + * subclass overrides any of the methods, false otherwise.
1.1675 + */
1.1676 + private static boolean auditSubclass(final Class subcl) {
1.1677 + Boolean result = AccessController.doPrivileged(
1.1678 + new PrivilegedAction<Boolean>() {
1.1679 + public Boolean run() {
1.1680 + for (Class cl = subcl;
1.1681 + cl != Thread.class;
1.1682 + cl = cl.getSuperclass())
1.1683 + {
1.1684 + try {
1.1685 + cl.getDeclaredMethod("getContextClassLoader", new Class[0]);
1.1686 + return Boolean.TRUE;
1.1687 + } catch (NoSuchMethodException ex) {
1.1688 + }
1.1689 + try {
1.1690 + Class[] params = {ClassLoader.class};
1.1691 + cl.getDeclaredMethod("setContextClassLoader", params);
1.1692 + return Boolean.TRUE;
1.1693 + } catch (NoSuchMethodException ex) {
1.1694 + }
1.1695 + }
1.1696 + return Boolean.FALSE;
1.1697 + }
1.1698 + }
1.1699 + );
1.1700 + return result.booleanValue();
1.1701 + }
1.1702 +
1.1703 + private native static StackTraceElement[][] dumpThreads(Thread[] threads);
1.1704 + private native static Thread[] getThreads();
1.1705 +
1.1706 + /**
1.1707 + * Returns the identifier of this Thread. The thread ID is a positive
1.1708 + * <tt>long</tt> number generated when this thread was created.
1.1709 + * The thread ID is unique and remains unchanged during its lifetime.
1.1710 + * When a thread is terminated, this thread ID may be reused.
1.1711 + *
1.1712 + * @return this thread's ID.
1.1713 + * @since 1.5
1.1714 + */
1.1715 + public long getId() {
1.1716 + return tid;
1.1717 + }
1.1718 +
1.1719 + /**
1.1720 + * A thread state. A thread can be in one of the following states:
1.1721 + * <ul>
1.1722 + * <li>{@link #NEW}<br>
1.1723 + * A thread that has not yet started is in this state.
1.1724 + * </li>
1.1725 + * <li>{@link #RUNNABLE}<br>
1.1726 + * A thread executing in the Java virtual machine is in this state.
1.1727 + * </li>
1.1728 + * <li>{@link #BLOCKED}<br>
1.1729 + * A thread that is blocked waiting for a monitor lock
1.1730 + * is in this state.
1.1731 + * </li>
1.1732 + * <li>{@link #WAITING}<br>
1.1733 + * A thread that is waiting indefinitely for another thread to
1.1734 + * perform a particular action is in this state.
1.1735 + * </li>
1.1736 + * <li>{@link #TIMED_WAITING}<br>
1.1737 + * A thread that is waiting for another thread to perform an action
1.1738 + * for up to a specified waiting time is in this state.
1.1739 + * </li>
1.1740 + * <li>{@link #TERMINATED}<br>
1.1741 + * A thread that has exited is in this state.
1.1742 + * </li>
1.1743 + * </ul>
1.1744 + *
1.1745 + * <p>
1.1746 + * A thread can be in only one state at a given point in time.
1.1747 + * These states are virtual machine states which do not reflect
1.1748 + * any operating system thread states.
1.1749 + *
1.1750 + * @since 1.5
1.1751 + * @see #getState
1.1752 + */
1.1753 + public enum State {
1.1754 + /**
1.1755 + * Thread state for a thread which has not yet started.
1.1756 + */
1.1757 + NEW,
1.1758 +
1.1759 + /**
1.1760 + * Thread state for a runnable thread. A thread in the runnable
1.1761 + * state is executing in the Java virtual machine but it may
1.1762 + * be waiting for other resources from the operating system
1.1763 + * such as processor.
1.1764 + */
1.1765 + RUNNABLE,
1.1766 +
1.1767 + /**
1.1768 + * Thread state for a thread blocked waiting for a monitor lock.
1.1769 + * A thread in the blocked state is waiting for a monitor lock
1.1770 + * to enter a synchronized block/method or
1.1771 + * reenter a synchronized block/method after calling
1.1772 + * {@link Object#wait() Object.wait}.
1.1773 + */
1.1774 + BLOCKED,
1.1775 +
1.1776 + /**
1.1777 + * Thread state for a waiting thread.
1.1778 + * A thread is in the waiting state due to calling one of the
1.1779 + * following methods:
1.1780 + * <ul>
1.1781 + * <li>{@link Object#wait() Object.wait} with no timeout</li>
1.1782 + * <li>{@link #join() Thread.join} with no timeout</li>
1.1783 + * <li>{@link LockSupport#park() LockSupport.park}</li>
1.1784 + * </ul>
1.1785 + *
1.1786 + * <p>A thread in the waiting state is waiting for another thread to
1.1787 + * perform a particular action.
1.1788 + *
1.1789 + * For example, a thread that has called <tt>Object.wait()</tt>
1.1790 + * on an object is waiting for another thread to call
1.1791 + * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
1.1792 + * that object. A thread that has called <tt>Thread.join()</tt>
1.1793 + * is waiting for a specified thread to terminate.
1.1794 + */
1.1795 + WAITING,
1.1796 +
1.1797 + /**
1.1798 + * Thread state for a waiting thread with a specified waiting time.
1.1799 + * A thread is in the timed waiting state due to calling one of
1.1800 + * the following methods with a specified positive waiting time:
1.1801 + * <ul>
1.1802 + * <li>{@link #sleep Thread.sleep}</li>
1.1803 + * <li>{@link Object#wait(long) Object.wait} with timeout</li>
1.1804 + * <li>{@link #join(long) Thread.join} with timeout</li>
1.1805 + * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
1.1806 + * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
1.1807 + * </ul>
1.1808 + */
1.1809 + TIMED_WAITING,
1.1810 +
1.1811 + /**
1.1812 + * Thread state for a terminated thread.
1.1813 + * The thread has completed execution.
1.1814 + */
1.1815 + TERMINATED;
1.1816 + }
1.1817 +
1.1818 + /**
1.1819 + * Returns the state of this thread.
1.1820 + * This method is designed for use in monitoring of the system state,
1.1821 + * not for synchronization control.
1.1822 + *
1.1823 + * @return this thread's state.
1.1824 + * @since 1.5
1.1825 + */
1.1826 + public State getState() {
1.1827 + // get current thread state
1.1828 + return sun.misc.VM.toThreadState(threadStatus);
1.1829 + }
1.1830 +
1.1831 + // Added in JSR-166
1.1832 +
1.1833 + /**
1.1834 + * Interface for handlers invoked when a <tt>Thread</tt> abruptly
1.1835 + * terminates due to an uncaught exception.
1.1836 + * <p>When a thread is about to terminate due to an uncaught exception
1.1837 + * the Java Virtual Machine will query the thread for its
1.1838 + * <tt>UncaughtExceptionHandler</tt> using
1.1839 + * {@link #getUncaughtExceptionHandler} and will invoke the handler's
1.1840 + * <tt>uncaughtException</tt> method, passing the thread and the
1.1841 + * exception as arguments.
1.1842 + * If a thread has not had its <tt>UncaughtExceptionHandler</tt>
1.1843 + * explicitly set, then its <tt>ThreadGroup</tt> object acts as its
1.1844 + * <tt>UncaughtExceptionHandler</tt>. If the <tt>ThreadGroup</tt> object
1.1845 + * has no
1.1846 + * special requirements for dealing with the exception, it can forward
1.1847 + * the invocation to the {@linkplain #getDefaultUncaughtExceptionHandler
1.1848 + * default uncaught exception handler}.
1.1849 + *
1.1850 + * @see #setDefaultUncaughtExceptionHandler
1.1851 + * @see #setUncaughtExceptionHandler
1.1852 + * @see ThreadGroup#uncaughtException
1.1853 + * @since 1.5
1.1854 + */
1.1855 + public interface UncaughtExceptionHandler {
1.1856 + /**
1.1857 + * Method invoked when the given thread terminates due to the
1.1858 + * given uncaught exception.
1.1859 + * <p>Any exception thrown by this method will be ignored by the
1.1860 + * Java Virtual Machine.
1.1861 + * @param t the thread
1.1862 + * @param e the exception
1.1863 + */
1.1864 + void uncaughtException(Thread t, Throwable e);
1.1865 + }
1.1866 +
1.1867 + // null unless explicitly set
1.1868 + private volatile UncaughtExceptionHandler uncaughtExceptionHandler;
1.1869 +
1.1870 + // null unless explicitly set
1.1871 + private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler;
1.1872 +
1.1873 + /**
1.1874 + * Set the default handler invoked when a thread abruptly terminates
1.1875 + * due to an uncaught exception, and no other handler has been defined
1.1876 + * for that thread.
1.1877 + *
1.1878 + * <p>Uncaught exception handling is controlled first by the thread, then
1.1879 + * by the thread's {@link ThreadGroup} object and finally by the default
1.1880 + * uncaught exception handler. If the thread does not have an explicit
1.1881 + * uncaught exception handler set, and the thread's thread group
1.1882 + * (including parent thread groups) does not specialize its
1.1883 + * <tt>uncaughtException</tt> method, then the default handler's
1.1884 + * <tt>uncaughtException</tt> method will be invoked.
1.1885 + * <p>By setting the default uncaught exception handler, an application
1.1886 + * can change the way in which uncaught exceptions are handled (such as
1.1887 + * logging to a specific device, or file) for those threads that would
1.1888 + * already accept whatever "default" behavior the system
1.1889 + * provided.
1.1890 + *
1.1891 + * <p>Note that the default uncaught exception handler should not usually
1.1892 + * defer to the thread's <tt>ThreadGroup</tt> object, as that could cause
1.1893 + * infinite recursion.
1.1894 + *
1.1895 + * @param eh the object to use as the default uncaught exception handler.
1.1896 + * If <tt>null</tt> then there is no default handler.
1.1897 + *
1.1898 + * @throws SecurityException if a security manager is present and it
1.1899 + * denies <tt>{@link RuntimePermission}
1.1900 + * ("setDefaultUncaughtExceptionHandler")</tt>
1.1901 + *
1.1902 + * @see #setUncaughtExceptionHandler
1.1903 + * @see #getUncaughtExceptionHandler
1.1904 + * @see ThreadGroup#uncaughtException
1.1905 + * @since 1.5
1.1906 + */
1.1907 + public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
1.1908 + SecurityManager sm = System.getSecurityManager();
1.1909 + if (sm != null) {
1.1910 + sm.checkPermission(
1.1911 + new RuntimePermission("setDefaultUncaughtExceptionHandler")
1.1912 + );
1.1913 + }
1.1914 +
1.1915 + defaultUncaughtExceptionHandler = eh;
1.1916 + }
1.1917 +
1.1918 + /**
1.1919 + * Returns the default handler invoked when a thread abruptly terminates
1.1920 + * due to an uncaught exception. If the returned value is <tt>null</tt>,
1.1921 + * there is no default.
1.1922 + * @since 1.5
1.1923 + * @see #setDefaultUncaughtExceptionHandler
1.1924 + */
1.1925 + public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){
1.1926 + return defaultUncaughtExceptionHandler;
1.1927 + }
1.1928 +
1.1929 + /**
1.1930 + * Returns the handler invoked when this thread abruptly terminates
1.1931 + * due to an uncaught exception. If this thread has not had an
1.1932 + * uncaught exception handler explicitly set then this thread's
1.1933 + * <tt>ThreadGroup</tt> object is returned, unless this thread
1.1934 + * has terminated, in which case <tt>null</tt> is returned.
1.1935 + * @since 1.5
1.1936 + */
1.1937 + public UncaughtExceptionHandler getUncaughtExceptionHandler() {
1.1938 + return uncaughtExceptionHandler != null ?
1.1939 + uncaughtExceptionHandler : group;
1.1940 + }
1.1941 +
1.1942 + /**
1.1943 + * Set the handler invoked when this thread abruptly terminates
1.1944 + * due to an uncaught exception.
1.1945 + * <p>A thread can take full control of how it responds to uncaught
1.1946 + * exceptions by having its uncaught exception handler explicitly set.
1.1947 + * If no such handler is set then the thread's <tt>ThreadGroup</tt>
1.1948 + * object acts as its handler.
1.1949 + * @param eh the object to use as this thread's uncaught exception
1.1950 + * handler. If <tt>null</tt> then this thread has no explicit handler.
1.1951 + * @throws SecurityException if the current thread is not allowed to
1.1952 + * modify this thread.
1.1953 + * @see #setDefaultUncaughtExceptionHandler
1.1954 + * @see ThreadGroup#uncaughtException
1.1955 + * @since 1.5
1.1956 + */
1.1957 + public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
1.1958 + checkAccess();
1.1959 + uncaughtExceptionHandler = eh;
1.1960 + }
1.1961 +
1.1962 + /**
1.1963 + * Dispatch an uncaught exception to the handler. This method is
1.1964 + * intended to be called only by the JVM.
1.1965 + */
1.1966 + private void dispatchUncaughtException(Throwable e) {
1.1967 + getUncaughtExceptionHandler().uncaughtException(this, e);
1.1968 + }
1.1969 +
1.1970 + /**
1.1971 + * Removes from the specified map any keys that have been enqueued
1.1972 + * on the specified reference queue.
1.1973 + */
1.1974 + static void processQueue(ReferenceQueue<Class<?>> queue,
1.1975 + ConcurrentMap<? extends
1.1976 + WeakReference<Class<?>>, ?> map)
1.1977 + {
1.1978 + Reference<? extends Class<?>> ref;
1.1979 + while((ref = queue.poll()) != null) {
1.1980 + map.remove(ref);
1.1981 + }
1.1982 + }
1.1983 +
1.1984 + /**
1.1985 + * Weak key for Class objects.
1.1986 + **/
1.1987 + static class WeakClassKey extends WeakReference<Class<?>> {
1.1988 + /**
1.1989 + * saved value of the referent's identity hash code, to maintain
1.1990 + * a consistent hash code after the referent has been cleared
1.1991 + */
1.1992 + private final int hash;
1.1993 +
1.1994 + /**
1.1995 + * Create a new WeakClassKey to the given object, registered
1.1996 + * with a queue.
1.1997 + */
1.1998 + WeakClassKey(Class<?> cl, ReferenceQueue<Class<?>> refQueue) {
1.1999 + super(cl, refQueue);
1.2000 + hash = System.identityHashCode(cl);
1.2001 + }
1.2002 +
1.2003 + /**
1.2004 + * Returns the identity hash code of the original referent.
1.2005 + */
1.2006 + @Override
1.2007 + public int hashCode() {
1.2008 + return hash;
1.2009 + }
1.2010 +
1.2011 + /**
1.2012 + * Returns true if the given object is this identical
1.2013 + * WeakClassKey instance, or, if this object's referent has not
1.2014 + * been cleared, if the given object is another WeakClassKey
1.2015 + * instance with the identical non-null referent as this one.
1.2016 + */
1.2017 + @Override
1.2018 + public boolean equals(Object obj) {
1.2019 + if (obj == this)
1.2020 + return true;
1.2021 +
1.2022 + if (obj instanceof WeakClassKey) {
1.2023 + Object referent = get();
1.2024 + return (referent != null) &&
1.2025 + (referent == ((WeakClassKey) obj).get());
1.2026 + } else {
1.2027 + return false;
1.2028 + }
1.2029 + }
1.2030 + }
1.2031 +
1.2032 + /* Some private helper methods */
1.2033 + private native void setPriority0(int newPriority);
1.2034 + private native void stop0(Object o);
1.2035 + private native void suspend0();
1.2036 + private native void resume0();
1.2037 + private native void interrupt0();
1.2038 +}