151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project 34e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.lang; 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkinimport dalvik.annotation.optimization.FastNative; 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.ref.Reference; 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.ref.ReferenceQueue; 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.ref.WeakReference; 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.AccessController; 3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.AccessControlContext; 3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.security.PrivilegedAction; 3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Map; 3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.HashMap; 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.concurrent.ConcurrentHashMap; 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.concurrent.ConcurrentMap; 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.concurrent.locks.LockSupport; 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.nio.ch.Interruptible; 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.reflect.CallerSensitive; 43a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport dalvik.system.VMStack; 44a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebskiimport libcore.util.EmptyArray; 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A <i>thread</i> is a thread of execution in a program. The Java 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Virtual Machine allows an application to have multiple threads of 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * execution running concurrently. 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Every thread has a priority. Threads with higher priority are 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * executed in preference to threads with lower priority. Each thread 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may or may not also be marked as a daemon. When code running in 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * some thread creates a new <code>Thread</code> object, the new 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread has its priority initially set equal to the priority of the 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * creating thread, and is a daemon thread if and only if the 5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * creating thread is a daemon. 5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * When a Java Virtual Machine starts up, there is usually a single 6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * non-daemon thread (which typically calls the method named 6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>main</code> of some designated class). The Java Virtual 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Machine continues to execute threads until either of the following 6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * occurs: 6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>The <code>exit</code> method of class <code>Runtime</code> has been 6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * called and the security manager has permitted the exit operation 6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to take place. 6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>All threads that are not daemon threads have died, either by 7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returning from the call to the <code>run</code> method or by 7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * throwing an exception that propagates beyond the <code>run</code> 7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method. 7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * There are two ways to create a new thread of execution. One is to 7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declare a class to be a subclass of <code>Thread</code>. This 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subclass should override the <code>run</code> method of class 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>Thread</code>. An instance of the subclass can then be 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * allocated and started. For example, a thread that computes primes 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * larger than a stated value could be written as follows: 814e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * <hr><blockquote><pre> 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class PrimeThread extends Thread { 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * long minPrime; 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * PrimeThread(long minPrime) { 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this.minPrime = minPrime; 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public void run() { 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * // compute primes larger than minPrime 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * . . . 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote><hr> 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The following code would then create a thread and start it running: 964e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * <blockquote><pre> 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * PrimeThread p = new PrimeThread(143); 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * p.start(); 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The other way to create a thread is to declare a class that 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements the <code>Runnable</code> interface. That class then 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implements the <code>run</code> method. An instance of the class can 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * then be allocated, passed as an argument when creating 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>Thread</code>, and started. The same example in this other 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * style looks like the following: 1074e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * <hr><blockquote><pre> 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class PrimeRun implements Runnable { 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * long minPrime; 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * PrimeRun(long minPrime) { 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this.minPrime = minPrime; 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * public void run() { 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * // compute primes larger than minPrime 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * . . . 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * } 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote><hr> 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The following code would then create a thread and start it running: 1224e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * <blockquote><pre> 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * PrimeRun p = new PrimeRun(143); 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * new Thread(p).start(); 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Every thread has a name for identification purposes. More than 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one thread may have the same name. If a name is not specified when 12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a thread is created, a new name is generated for it. 13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Unless otherwise noted, passing a {@code null} argument to a constructor 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or method in this class will cause a {@link NullPointerException} to be 13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thrown. 13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author unascribed 13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Runnable 13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Runtime#exit(int) 13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #run() 13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #stop() 14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0 14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic 14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiclass Thread implements Runnable { 14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Make sure registerNatives is the first thing <clinit> does. */ 14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 146a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 147a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * The synchronization object responsible for this thread's join/sleep/park operations. 148a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 149a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private final Object lock = new Object(); 150a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 151a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private volatile long nativePeer; 152a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 153a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski boolean started = false; 154a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1554e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak private volatile String name; 156a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private int priority; 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Thread threadQ; 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private long eetop; 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Whether or not to single_step this thread. */ 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private boolean single_step; 16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Whether or not the thread is a daemon thread. */ 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private boolean daemon = false; 16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* JVM state */ 16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private boolean stillborn = false; 16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* What will be run. */ 17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Runnable target; 17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* The group of this thread */ 17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private ThreadGroup group; 17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* The context ClassLoader for this thread */ 17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private ClassLoader contextClassLoader; 17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* The inherited AccessControlContext of this thread */ 18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private AccessControlContext inheritedAccessControlContext; 18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* For autonumbering anonymous threads. */ 18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int threadInitNumber; 18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static synchronized int nextThreadNum() { 18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return threadInitNumber++; 18651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* ThreadLocal values pertaining to this thread. This map is maintained 18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the ThreadLocal class. */ 19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ThreadLocal.ThreadLocalMap threadLocals = null; 19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * InheritableThreadLocal values pertaining to this thread. This map is 19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * maintained by the InheritableThreadLocal class. 19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; 19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The requested stack size for this thread, or 0 if the creator did 20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not specify a stack size. It is up to the VM to do whatever it 20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * likes with this number; some VMs will ignore it. 20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private long stackSize; 20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * JVM-private state that persists after native thread termination. 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private long nativeParkEventPointer; 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Thread ID 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private long tid; 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* For generating thread ID */ 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static long threadSeqNumber; 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Java thread status for tools, 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * initialized to indicate thread 'not yet started' 22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private volatile int threadStatus = 0; 22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static synchronized long nextThreadID() { 22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ++threadSeqNumber; 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The argument supplied to the current call to 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.util.concurrent.locks.LockSupport.park. 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Set by (private) java.util.concurrent.locks.LockSupport.setBlocker 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Accessed using java.util.concurrent.locks.LockSupport.getBlocker 23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski volatile Object parkBlocker; 23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* The object in which this thread is blocked in an interruptible I/O 23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operation, if any. The blocker's interrupt method should be invoked 23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * after setting this thread's interrupt status. 24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private volatile Interruptible blocker; 24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private final Object blockerLock = new Object(); 24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 244a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 245a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Set the blocker field; invoked via sun.misc.SharedSecrets from java.nio code 246a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 247a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide 24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 249a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public void blockedOn(Interruptible b) { 25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski synchronized (blockerLock) { 25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski blocker = b; 25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The minimum priority that a thread can have. 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final static int MIN_PRIORITY = 1; 25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The default priority that is assigned to a thread. 26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final static int NORM_PRIORITY = 5; 26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The maximum priority that a thread can have. 26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final static int MAX_PRIORITY = 10; 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a reference to the currently executing thread object. 27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the currently executing thread. 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 275fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static native Thread currentThread(); 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A hint to the scheduler that the current thread is willing to yield 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * its current use of a processor. The scheduler is free to ignore this 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * hint. 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Yield is a heuristic attempt to improve relative progression 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * between threads that would otherwise over-utilise a CPU. Its use 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * should be combined with detailed profiling and benchmarking to 28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ensure that it actually has the desired effect. 28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> It is rarely appropriate to use this method. It may be useful 28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for debugging or testing purposes, where it may help to reproduce 29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * bugs due to race conditions. It may also be useful when designing 29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * concurrency control constructs such as the ones in the 29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.util.concurrent.locks} package. 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static native void yield(); 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Causes the currently executing thread to sleep (temporarily cease 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * execution) for the specified number of milliseconds, subject to 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the precision and accuracy of system timers and schedulers. The thread 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * does not lose ownership of any monitors. 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param millis 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of time to sleep in milliseconds 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the value of {@code millis} is negative 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws InterruptedException 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any thread has interrupted the current thread. The 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>interrupted status</i> of the current thread is 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * cleared when this exception is thrown. 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 313a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public static void sleep(long millis) throws InterruptedException { 314a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Thread.sleep(millis, 0); 315a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 316a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 317fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 318a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static native void sleep(Object lock, long millis, int nanos) 319a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throws InterruptedException; 32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Causes the currently executing thread to sleep (temporarily cease 32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * execution) for the specified number of milliseconds plus the specified 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * number of nanoseconds, subject to the precision and accuracy of system 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * timers and schedulers. The thread does not lose ownership of any 32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * monitors. 32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param millis 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of time to sleep in milliseconds 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param nanos 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0-999999} additional nanoseconds to sleep 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the value of {@code millis} is negative, or the value of 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code nanos} is not in the range {@code 0-999999} 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws InterruptedException 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any thread has interrupted the current thread. The 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>interrupted status</i> of the current thread is 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * cleared when this exception is thrown. 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sleep(long millis, int nanos) 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws InterruptedException { 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (millis < 0) { 346a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new IllegalArgumentException("millis < 0: " + millis); 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 348a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (nanos < 0) { 349a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new IllegalArgumentException("nanos < 0: " + nanos); 350a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 351a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (nanos > 999999) { 352a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new IllegalArgumentException("nanos > 999999: " + nanos); 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 355a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // The JLS 3rd edition, section 17.9 says: "...sleep for zero 356a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // time...need not have observable effects." 357a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (millis == 0 && nanos == 0) { 358a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // ...but we still have to handle being interrupted. 359a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (Thread.interrupted()) { 360a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new InterruptedException(); 361a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 362a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return; 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 365a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski long start = System.nanoTime(); 366a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski long duration = (millis * NANOS_PER_MILLI) + nanos; 367a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 368a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Object lock = currentThread().lock; 369a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 370a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Wait may return early, so loop until sleep duration passes. 371a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (lock) { 372a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski while (true) { 373a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski sleep(lock, millis, nanos); 374a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 375a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski long now = System.nanoTime(); 376a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski long elapsed = now - start; 377a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 378a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (elapsed >= duration) { 379a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski break; 380a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 381a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 382a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski duration -= elapsed; 383a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski start = now; 384a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski millis = duration / NANOS_PER_MILLI; 385a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski nanos = (int) (duration % NANOS_PER_MILLI); 386a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 387a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Initializes a Thread. 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param g the Thread group 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param target the object whose run() method gets called 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the new Thread 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param stackSize the desired stack size for the new thread, or 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * zero to indicate that this parameter is to be ignored. 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 39946c38b30477d579beaa528386f71814c0191a48fNarayan Kamath private void init(ThreadGroup g, Runnable target, String name, long stackSize) { 40046c38b30477d579beaa528386f71814c0191a48fNarayan Kamath Thread parent = currentThread(); 40146c38b30477d579beaa528386f71814c0191a48fNarayan Kamath if (g == null) { 40246c38b30477d579beaa528386f71814c0191a48fNarayan Kamath g = parent.getThreadGroup(); 40346c38b30477d579beaa528386f71814c0191a48fNarayan Kamath } 40446c38b30477d579beaa528386f71814c0191a48fNarayan Kamath 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski g.addUnstarted(); 40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.group = g; 407a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.target = target; 40946c38b30477d579beaa528386f71814c0191a48fNarayan Kamath this.priority = parent.getPriority(); 41046c38b30477d579beaa528386f71814c0191a48fNarayan Kamath this.daemon = parent.isDaemon(); 41146c38b30477d579beaa528386f71814c0191a48fNarayan Kamath setName(name); 41246c38b30477d579beaa528386f71814c0191a48fNarayan Kamath 41346c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init2(parent); 414a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Stash the specified stack size in case the VM cares */ 41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.stackSize = stackSize; 41746c38b30477d579beaa528386f71814c0191a48fNarayan Kamath tid = nextThreadID(); 41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Throws CloneNotSupportedException as a Thread can not be meaningfully 42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * cloned. Construct a new Thread instead. 42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws CloneNotSupportedException 42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * always 42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Override 42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected Object clone() throws CloneNotSupportedException { 42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new CloneNotSupportedException(); 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Allocates a new {@code Thread} object. This constructor has the same 43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code (null, null, gname)}, where {@code gname} is a newly generated 43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name. Automatically generated names are of the form 43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer. 43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Thread() { 44046c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init(null, null, "Thread-" + nextThreadNum(), 0); 44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Allocates a new {@code Thread} object. This constructor has the same 44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code (null, target, gname)}, where {@code gname} is a newly generated 44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name. Automatically generated names are of the form 44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer. 44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param target 45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the object whose {@code run} method is invoked when this thread 45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is started. If {@code null}, this classes {@code run} method does 45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * nothing. 45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Thread(Runnable target) { 45646c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init(null, target, "Thread-" + nextThreadNum(), 0); 45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Allocates a new {@code Thread} object. This constructor has the same 46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code (group, target, gname)} ,where {@code gname} is a newly generated 46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * name. Automatically generated names are of the form 46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer. 46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param group 46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the thread group. If {@code null} and there is a security 46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager, the group is determined by {@linkplain 46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}. 47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If there is not a security manager or {@code 47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager.getThreadGroup()} returns {@code null}, the group 47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is set to the current thread's thread group. 47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param target 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the object whose {@code run} method is invoked when this thread 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is started. If {@code null}, this thread's run method is invoked. 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the current thread cannot create a thread in the specified 48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread group 48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Thread(ThreadGroup group, Runnable target) { 48346c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init(group, target, "Thread-" + nextThreadNum(), 0); 48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Allocates a new {@code Thread} object. This constructor has the same 48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code (null, null, name)}. 49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name 49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the name of the new thread 49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Thread(String name) { 49546c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init(null, null, name, 0); 49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Allocates a new {@code Thread} object. This constructor has the same 50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code (group, null, name)}. 50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param group 50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the thread group. If {@code null} and there is a security 50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager, the group is determined by {@linkplain 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}. 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If there is not a security manager or {@code 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager.getThreadGroup()} returns {@code null}, the group 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is set to the current thread's thread group. 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the name of the new thread 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the current thread cannot create a thread in the specified 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread group 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Thread(ThreadGroup group, String name) { 51946c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init(group, null, name, 0); 520a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 521a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 52246c38b30477d579beaa528386f71814c0191a48fNarayan Kamath 523a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** @hide */ 5246975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-added: Private constructor - used by the runtime. 525a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Thread(ThreadGroup group, String name, int priority, boolean daemon) { 52646c38b30477d579beaa528386f71814c0191a48fNarayan Kamath this.group = group; 52746c38b30477d579beaa528386f71814c0191a48fNarayan Kamath this.group.addUnstarted(); 52846c38b30477d579beaa528386f71814c0191a48fNarayan Kamath // Must be tolerant of threads without a name. 52946c38b30477d579beaa528386f71814c0191a48fNarayan Kamath if (name == null) { 53046c38b30477d579beaa528386f71814c0191a48fNarayan Kamath name = "Thread-" + nextThreadNum(); 53146c38b30477d579beaa528386f71814c0191a48fNarayan Kamath } 53281888044c73382ef87285593afdf80e840288b06Narayan Kamath 53381888044c73382ef87285593afdf80e840288b06Narayan Kamath // NOTE: Resist the temptation to call setName() here. This constructor is only called 53481888044c73382ef87285593afdf80e840288b06Narayan Kamath // by the runtime to construct peers for threads that have attached via JNI and it's 53581888044c73382ef87285593afdf80e840288b06Narayan Kamath // undesirable to clobber their natively set name. 53681888044c73382ef87285593afdf80e840288b06Narayan Kamath this.name = name; 53781888044c73382ef87285593afdf80e840288b06Narayan Kamath 53846c38b30477d579beaa528386f71814c0191a48fNarayan Kamath this.priority = priority; 53946c38b30477d579beaa528386f71814c0191a48fNarayan Kamath this.daemon = daemon; 54046c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init2(currentThread()); 54146c38b30477d579beaa528386f71814c0191a48fNarayan Kamath tid = nextThreadID(); 54246c38b30477d579beaa528386f71814c0191a48fNarayan Kamath } 54346c38b30477d579beaa528386f71814c0191a48fNarayan Kamath 54446c38b30477d579beaa528386f71814c0191a48fNarayan Kamath private void init2(Thread parent) { 54546c38b30477d579beaa528386f71814c0191a48fNarayan Kamath this.contextClassLoader = parent.getContextClassLoader(); 54646c38b30477d579beaa528386f71814c0191a48fNarayan Kamath this.inheritedAccessControlContext = AccessController.getContext(); 54746c38b30477d579beaa528386f71814c0191a48fNarayan Kamath if (parent.inheritableThreadLocals != null) { 54846c38b30477d579beaa528386f71814c0191a48fNarayan Kamath this.inheritableThreadLocals = ThreadLocal.createInheritedMap( 54946c38b30477d579beaa528386f71814c0191a48fNarayan Kamath parent.inheritableThreadLocals); 55046c38b30477d579beaa528386f71814c0191a48fNarayan Kamath } 55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Allocates a new {@code Thread} object. This constructor has the same 55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} 55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code (null, target, name)}. 55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param target 55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the object whose {@code run} method is invoked when this thread 56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is started. If {@code null}, this thread's run method is invoked. 56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name 56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the name of the new thread 56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Thread(Runnable target, String name) { 56646c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init(null, target, name, 0); 56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Allocates a new {@code Thread} object so that it has {@code target} 57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as its run object, has the specified {@code name} as its name, 57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and belongs to the thread group referred to by {@code group}. 57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If there is a security manager, its 57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link SecurityManager#checkAccess(ThreadGroup) checkAccess} 57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is invoked with the ThreadGroup as its argument. 57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>In addition, its {@code checkPermission} method is invoked with 57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the {@code RuntimePermission("enableContextClassLoaderOverride")} 58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * permission when invoked directly or indirectly by the constructor 58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of a subclass which overrides the {@code getContextClassLoader} 58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code setContextClassLoader} methods. 58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The priority of the newly created thread is set equal to the 58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * priority of the thread creating it, that is, the currently running 58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread. The method {@linkplain #setPriority setPriority} may be 58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * used to change the priority to a new value. 58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The newly created thread is initially marked as being a daemon 59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread if and only if the thread creating it is currently marked 59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as a daemon thread. The method {@linkplain #setDaemon setDaemon} 59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be used to change whether or not a thread is a daemon. 59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param group 59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the thread group. If {@code null} and there is a security 59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager, the group is determined by {@linkplain 59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}. 59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If there is not a security manager or {@code 59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager.getThreadGroup()} returns {@code null}, the group 60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is set to the current thread's thread group. 60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param target 60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the object whose {@code run} method is invoked when this thread 60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is started. If {@code null}, this thread's run method is invoked. 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name 60751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the name of the new thread 60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the current thread cannot create a thread in the specified 61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread group or cannot override the context class loader methods. 61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Thread(ThreadGroup group, Runnable target, String name) { 61446c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init(group, target, name, 0); 61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Allocates a new {@code Thread} object so that it has {@code target} 61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as its run object, has the specified {@code name} as its name, 62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and belongs to the thread group referred to by {@code group}, and has 62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified <i>stack size</i>. 62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This constructor is identical to {@link 62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * #Thread(ThreadGroup,Runnable,String)} with the exception of the fact 62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that it allows the thread stack size to be specified. The stack size 62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is the approximate number of bytes of address space that the virtual 62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * machine is to allocate for this thread's stack. <b>The effect of the 62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code stackSize} parameter, if any, is highly platform dependent.</b> 62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>On some platforms, specifying a higher value for the 63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code stackSize} parameter may allow a thread to achieve greater 63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * recursion depth before throwing a {@link StackOverflowError}. 63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Similarly, specifying a lower value may allow a greater number of 63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * threads to exist concurrently without throwing an {@link 63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * OutOfMemoryError} (or other internal error). The details of 63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the relationship between the value of the <tt>stackSize</tt> parameter 63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and the maximum recursion depth and concurrency level are 63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * platform-dependent. <b>On some platforms, the value of the 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code stackSize} parameter may have no effect whatsoever.</b> 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The virtual machine is free to treat the {@code stackSize} 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * parameter as a suggestion. If the specified value is unreasonably low 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for the platform, the virtual machine may instead use some 64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * platform-specific minimum value; if the specified value is unreasonably 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * high, the virtual machine may instead use some platform-specific 64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * maximum. Likewise, the virtual machine is free to round the specified 64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value up or down as it sees fit (or to ignore it completely). 64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Specifying a value of zero for the {@code stackSize} parameter will 65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * cause this constructor to behave exactly like the 65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code Thread(ThreadGroup, Runnable, String)} constructor. 65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p><i>Due to the platform-dependent nature of the behavior of this 65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constructor, extreme care should be exercised in its use. 65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The thread stack size necessary to perform a given computation will 65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * likely vary from one JRE implementation to another. In light of this 65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * variation, careful tuning of the stack size parameter may be required, 65851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and the tuning may need to be repeated for each JRE implementation on 65951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * which an application is to run.</i> 66051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 66151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: Java platform implementers are encouraged to 66251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * document their implementation's behavior with respect to the 66351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code stackSize} parameter. 66451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 66551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param group 66751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the thread group. If {@code null} and there is a security 66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * manager, the group is determined by {@linkplain 66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}. 67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If there is not a security manager or {@code 67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager.getThreadGroup()} returns {@code null}, the group 67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is set to the current thread's thread group. 67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param target 67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the object whose {@code run} method is invoked when this thread 67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is started. If {@code null}, this thread's run method is invoked. 67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the name of the new thread 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param stackSize 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the desired stack size for the new thread, or zero to indicate 68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this parameter is to be ignored. 68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the current thread cannot create a thread in the specified 68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread group 68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Thread(ThreadGroup group, Runnable target, String name, 69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long stackSize) { 69346c38b30477d579beaa528386f71814c0191a48fNarayan Kamath init(group, target, name, stackSize); 69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Causes this thread to begin execution; the Java Virtual Machine 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * calls the <code>run</code> method of this thread. 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The result is that two threads are running concurrently: the 70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * current thread (which returns from the call to the 70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>start</code> method) and the other thread (which executes its 70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>run</code> method). 70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * It is never legal to start a thread more than once. 70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * In particular, a thread may not be restarted once it has completed 70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * execution. 70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalThreadStateException if the thread was already 71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * started. 71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #run() 71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #stop() 71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public synchronized void start() { 71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method is not invoked for the main method thread or "system" 71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * group threads created/set up by the VM. Any new functionality added 71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to this method in the future may have to also be added to the VM. 71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A zero status value corresponds to state "NEW". 72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 7224d8dc721410a0bf120893ded7a9bf684a962de54Artem Udovichenko // Android-changed: throw if 'started' is true 7234d8dc721410a0bf120893ded7a9bf684a962de54Artem Udovichenko if (threadStatus != 0 || started) 72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalThreadStateException(); 72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Notify the group that this thread is about to be started 72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so that it can be added to the group's list of threads 72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and the group's unstarted count can be decremented. */ 72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski group.add(this); 73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 731a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski started = false; 73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 733a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski nativeCreate(this, stackSize, daemon); 73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski started = true; 73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } finally { 73651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!started) { 73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski group.threadStartFailed(this); 73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } catch (Throwable ignore) { 74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* do nothing. If start0 threw a Throwable then 74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski it will be passed up the call stack */ 74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 747a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native static void nativeCreate(Thread t, long stackSize, boolean daemon); 74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this thread was constructed using a separate 75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>Runnable</code> run object, then that 75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>Runnable</code> object's <code>run</code> method is called; 75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, this method does nothing and returns. 75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Subclasses of <code>Thread</code> should override this method. 75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #start() 75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #stop() 75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #Thread(ThreadGroup, Runnable, String) 76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Override 76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public void run() { 76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (target != null) { 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski target.run(); 76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method is called by the system to give a Thread 77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a chance to clean up before it actually exits. 77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private void exit() { 77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (group != null) { 77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski group.threadTerminated(this); 77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski group = null; 77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Aggressively null out all reference fields: see bug 4006245 */ 77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski target = null; 77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Speed the release of some of these resources */ 78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski threadLocals = null; 78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski inheritableThreadLocals = null; 78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski inheritedAccessControlContext = null; 78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski blocker = null; 78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski uncaughtExceptionHandler = null; 78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Forces the thread to stop executing. 78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If there is a security manager installed, its <code>checkAccess</code> 79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is called with <code>this</code> 79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as its argument. This may result in a 79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>SecurityException</code> being raised (in the current thread). 79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this thread is different from the current thread (that is, the current 79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread is trying to stop a thread other than itself), the 79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * security manager's <code>checkPermission</code> method (with a 79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>RuntimePermission("stopThread")</code> argument) is called in 79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * addition. 80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Again, this may result in throwing a 80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>SecurityException</code> (in the current thread). 80251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The thread represented by this thread is forced to stop whatever 80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is doing abnormally and to throw a newly created 80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>ThreadDeath</code> object as an exception. 80651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * It is permitted to stop a thread that has not yet been started. 80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the thread is eventually started, it immediately terminates. 80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * An application should not normally try to catch 81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>ThreadDeath</code> unless it must do some extraordinary 81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * cleanup operation (note that the throwing of 81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>ThreadDeath</code> causes <code>finally</code> clauses of 81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>try</code> statements to be executed before the thread 81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * officially dies). If a <code>catch</code> clause catches a 81651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>ThreadDeath</code> object, it is important to rethrow the 81751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object so that the thread actually dies. 81851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 81951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The top-level error handler that reacts to otherwise uncaught 82051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exceptions does not print out a message or otherwise notify the 82151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * application if the uncaught exception is an instance of 82251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>ThreadDeath</code>. 82351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 82451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException if the current thread cannot 82551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * modify this thread. 82651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #interrupt() 82751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #checkAccess() 82851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #run() 82951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #start() 83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see ThreadDeath 83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see ThreadGroup#uncaughtException(Thread,Throwable) 83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkAccess(Thread) 83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @deprecated This method is inherently unsafe. Stopping a thread with 83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Thread.stop causes it to unlock all of the monitors that it 83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has locked (as a natural consequence of the unchecked 83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>ThreadDeath</code> exception propagating up the stack). If 83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * any of the objects previously protected by these monitors were in 83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an inconsistent state, the damaged objects become visible to 84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other threads, potentially resulting in arbitrary behavior. Many 84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * uses of <code>stop</code> should be replaced by code that simply 84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * modifies some variable to indicate that the target thread should 84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * stop running. The target thread should check this variable 84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * regularly, and return from its run method in an orderly fashion 84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the variable indicates that it is to stop running. If the 84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * target thread waits for long periods (on a condition variable, 84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for example), the <code>interrupt</code> method should be used to 84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interrupt the wait. 84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For more information, see 850309f9df28350e15445b9135e8b710fa2b34b5dc1Yi Kong * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why 85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Deprecated 85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void stop() { 85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski stop(new ThreadDeath()); 85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 8594e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * Throws {@code UnsupportedOperationException}. 86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 8614e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * @param obj ignored 8624e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * 8634e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * @deprecated This method was originally designed to force a thread to stop 8644e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * and throw a given {@code Throwable} as an exception. It was 8654e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * inherently unsafe (see {@link #stop()} for details), and furthermore 8664e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * could be used to generate exceptions that the target thread was 8674e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * not prepared to handle. 86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For more information, see 869309f9df28350e15445b9135e8b710fa2b34b5dc1Yi Kong * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why 87051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 87151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 87251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Deprecated 873a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public final void stop(Throwable obj) { 874a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new UnsupportedOperationException(); 87551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Interrupts this thread. 87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Unless the current thread is interrupting itself, which is 88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * always permitted, the {@link #checkAccess() checkAccess} method 88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this thread is invoked, which may cause a {@link 88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityException} to be thrown. 88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this thread is blocked in an invocation of the {@link 88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link 88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Object#wait(long, int) wait(long, int)} methods of the {@link Object} 88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class, or of the {@link #join()}, {@link #join(long)}, {@link 88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)}, 89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * methods of this class, then its interrupt status will be cleared and it 89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will receive an {@link InterruptedException}. 89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this thread is blocked in an I/O operation upon an {@link 8944e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * java.nio.channels.InterruptibleChannel InterruptibleChannel} 8954e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * then the channel will be closed, the thread's interrupt 89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status will be set, and the thread will receive a {@link 89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.nio.channels.ClosedByInterruptException}. 89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 89951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this thread is blocked in a {@link java.nio.channels.Selector} 90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * then the thread's interrupt status will be set and it will return 90151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * immediately from the selection operation, possibly with a non-zero 90251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value, just as if the selector's {@link 90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.nio.channels.Selector#wakeup wakeup} method were invoked. 90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 90551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If none of the previous conditions hold then this thread's interrupt 90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status will be set. </p> 90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 90851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Interrupting a thread that is not alive need not have any effect. 90951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 91051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the current thread cannot modify this thread 91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @revised 6.0 91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @spec JSR-51 91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 91651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public void interrupt() { 91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (this != Thread.currentThread()) 91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski checkAccess(); 91951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 92051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski synchronized (blockerLock) { 92151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Interruptible b = blocker; 92251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (b != null) { 923a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski nativeInterrupt(); 92451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.interrupt(this); 92551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 92651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 92751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 928a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski nativeInterrupt(); 92951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 93051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 93151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 93251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Tests whether the current thread has been interrupted. The 93351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>interrupted status</i> of the thread is cleared by this method. In 93451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other words, if this method were to be called twice in succession, the 93551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * second call would return false (unless the current thread were 93651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interrupted again, after the first call had cleared its interrupted 93751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status and before the second call had examined it). 93851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>A thread interruption ignored because a thread was not alive 94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * at the time of the interrupt will be reflected by this method 94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returning false. 94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <code>true</code> if the current thread has been interrupted; 94451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>false</code> otherwise. 94551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #isInterrupted() 94651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @revised 6.0 94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 948fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 949a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public static native boolean interrupted(); 95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 95251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Tests whether this thread has been interrupted. The <i>interrupted 95351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status</i> of the thread is unaffected by this method. 95451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>A thread interruption ignored because a thread was not alive 95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * at the time of the interrupt will be reflected by this method 95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returning false. 95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <code>true</code> if this thread has been interrupted; 96051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>false</code> otherwise. 96151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #interrupted() 96251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @revised 6.0 96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 964fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 965a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public native boolean isInterrupted(); 96651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 96751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 96846c38b30477d579beaa528386f71814c0191a48fNarayan Kamath * Throws {@link UnsupportedOperationException}. 96951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 97051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @deprecated This method was originally designed to destroy this 97151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread without any cleanup. Any monitors it held would have 97251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * remained locked. However, the method was never implemented. 97351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If if were to be implemented, it would be deadlock-prone in 97451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * much the manner of {@link #suspend}. If the target thread held 97551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a lock protecting a critical system resource when it was 97651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * destroyed, no thread could ever access this resource again. 97751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If another thread ever attempted to lock this resource, deadlock 97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * would result. Such deadlocks typically manifest themselves as 97951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "frozen" processes. For more information, see 980309f9df28350e15445b9135e8b710fa2b34b5dc1Yi Kong * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides/concurrency/threadPrimitiveDeprecation.html"> 98151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 98246c38b30477d579beaa528386f71814c0191a48fNarayan Kamath * @throws UnsupportedOperationException always 98351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 9846975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Throw UnsupportedOperationException instead of 98546c38b30477d579beaa528386f71814c0191a48fNarayan Kamath // NoSuchMethodError. 98651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Deprecated 98751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public void destroy() { 98846c38b30477d579beaa528386f71814c0191a48fNarayan Kamath throw new UnsupportedOperationException(); 98951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 99051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 99151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 99251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Tests if this thread is alive. A thread is alive if it has 99351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * been started and has not yet died. 99451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <code>true</code> if this thread is alive; 99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>false</code> otherwise. 99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 998a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public final boolean isAlive() { 999a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return nativePeer != 0; 1000a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Suspends this thread. 100451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 100551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * First, the <code>checkAccess</code> method of this thread is called 100651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with no arguments. This may result in throwing a 100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>SecurityException </code>(in the current thread). 100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 100951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the thread is alive, it is suspended and makes no further 101051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * progress unless and until it is resumed. 101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 101251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException if the current thread cannot modify 101351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this thread. 101451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #checkAccess 101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @deprecated This method has been deprecated, as it is 101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * inherently deadlock-prone. If the target thread holds a lock on the 101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * monitor protecting a critical system resource when it is suspended, no 101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread can access this resource until the target thread is resumed. If 101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the thread that would resume the target thread attempts to lock this 102051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * monitor prior to calling <code>resume</code>, deadlock results. Such 102151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * deadlocks typically manifest themselves as "frozen" processes. 102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For more information, see 1023309f9df28350e15445b9135e8b710fa2b34b5dc1Yi Kong * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why 102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Deprecated 102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void suspend() { 1028a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new UnsupportedOperationException(); 102951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 103051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 103151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 103251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Resumes a suspended thread. 103351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 103451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * First, the <code>checkAccess</code> method of this thread is called 103551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with no arguments. This may result in throwing a 103651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>SecurityException</code> (in the current thread). 103751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 103851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the thread is alive but suspended, it is resumed and is 103951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * permitted to make progress in its execution. 104051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 104151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException if the current thread cannot modify this 104251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread. 104351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #checkAccess 104451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #suspend() 104551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @deprecated This method exists solely for use with {@link #suspend}, 104651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * which has been deprecated because it is deadlock-prone. 104751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For more information, see 1048309f9df28350e15445b9135e8b710fa2b34b5dc1Yi Kong * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why 104951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 105051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 105151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Deprecated 105251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void resume() { 1053a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new UnsupportedOperationException(); 105451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 105551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 105651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 105751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Changes the priority of this thread. 105851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 105951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * First the <code>checkAccess</code> method of this thread is called 106051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with no arguments. This may result in throwing a 106151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>SecurityException</code>. 106251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 106351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Otherwise, the priority of this thread is set to the smaller of 106451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified <code>newPriority</code> and the maximum permitted 106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * priority of the thread's thread group. 106651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 106751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newPriority priority to set this thread to 106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException If the priority is not in the 106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range <code>MIN_PRIORITY</code> to 107051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>MAX_PRIORITY</code>. 107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException if the current thread cannot modify 107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this thread. 107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #getPriority 107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #checkAccess() 107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #getThreadGroup() 107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #MAX_PRIORITY 107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #MIN_PRIORITY 107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see ThreadGroup#getMaxPriority() 107951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 108051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void setPriority(int newPriority) { 108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ThreadGroup g; 108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski checkAccess(); 108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { 1084087345a1b9523ee891289afebeb61368f6466f84Narayan Kamath // Android-changed: Improve exception message when the new priority 1085087345a1b9523ee891289afebeb61368f6466f84Narayan Kamath // is out of bounds. 1086087345a1b9523ee891289afebeb61368f6466f84Narayan Kamath throw new IllegalArgumentException("Priority out of range: " + newPriority); 108751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 108851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if((g = getThreadGroup()) != null) { 108951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newPriority > g.getMaxPriority()) { 109051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski newPriority = g.getMaxPriority(); 109151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1092a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized(this) { 1093a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski this.priority = newPriority; 1094a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isAlive()) { 1095a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski nativeSetPriority(newPriority); 1096a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1097a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 109851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 109951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 110051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 110151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 110251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns this thread's priority. 110351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 110451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this thread's priority. 110551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setPriority 110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final int getPriority() { 110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return priority; 110951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 111251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Changes the name of this thread to be equal to the argument 111351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>name</code>. 111451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 111551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * First the <code>checkAccess</code> method of this thread is called 111651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with no arguments. This may result in throwing a 111751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>SecurityException</code>. 111851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the new name for this thread. 112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException if the current thread cannot modify this 112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread. 112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #getName 112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #checkAccess() 112451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void setName(String name) { 112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski checkAccess(); 112746c38b30477d579beaa528386f71814c0191a48fNarayan Kamath if (name == null) { 112846c38b30477d579beaa528386f71814c0191a48fNarayan Kamath throw new NullPointerException("name == null"); 112946c38b30477d579beaa528386f71814c0191a48fNarayan Kamath } 113046c38b30477d579beaa528386f71814c0191a48fNarayan Kamath 1131a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized (this) { 1132a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski this.name = name; 1133a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (isAlive()) { 1134a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski nativeSetName(name); 1135a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1136a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 113751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns this thread's name. 114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this thread's name. 114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setName(String) 114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final String getName() { 1146a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return name; 114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 114951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the thread group to which this thread belongs. 115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method returns null if this thread has died 115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (been stopped). 115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this thread's thread group. 115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final ThreadGroup getThreadGroup() { 115746c38b30477d579beaa528386f71814c0191a48fNarayan Kamath // Android-changed: Return null if the thread is terminated. 115846c38b30477d579beaa528386f71814c0191a48fNarayan Kamath if (getState() == Thread.State.TERMINATED) { 115946c38b30477d579beaa528386f71814c0191a48fNarayan Kamath return null; 116046c38b30477d579beaa528386f71814c0191a48fNarayan Kamath } 116151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return group; 116251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 116451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 116551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an estimate of the number of active threads in the current 116651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread's {@linkplain java.lang.ThreadGroup thread group} and its 116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subgroups. Recursively iterates over all subgroups in the current 116851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread's thread group. 116951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The value returned is only an estimate because the number of 117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * threads may change dynamically while this method traverses internal 117251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * data structures, and might be affected by the presence of certain 117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * system threads. This method is intended primarily for debugging 117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and monitoring purposes. 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an estimate of the number of active threads in the current 117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread's thread group and in any other thread group that 117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has the current thread's thread group as an ancestor 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int activeCount() { 118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return currentThread().getThreadGroup().activeCount(); 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies into the specified array every active thread in the current 118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread's thread group and its subgroups. This method simply 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invokes the {@link java.lang.ThreadGroup#enumerate(Thread[])} 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method of the current thread's thread group. 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> An application might use the {@linkplain #activeCount activeCount} 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method to get an estimate of how big the array should be, however 119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>if the array is too short to hold all the threads, the extra threads 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are silently ignored.</i> If it is critical to obtain every active 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread in the current thread's thread group and its subgroups, the 119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invoker should verify that the returned int value is strictly less 119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * than the length of {@code tarray}. 119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Due to the inherent race condition in this method, it is recommended 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that the method only be used for debugging and monitoring purposes. 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param tarray 120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array into which to put the list of threads 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the number of threads put into the array 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@link java.lang.ThreadGroup#checkAccess} determines that 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current thread cannot access its thread group 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int enumerate(Thread tarray[]) { 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return currentThread().getThreadGroup().enumerate(tarray); 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Counts the number of stack frames in this thread. The thread must 121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be suspended. 121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the number of stack frames in this thread. 121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalThreadStateException if this thread is not 122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * suspended. 122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @deprecated The definition of this call depends on {@link #suspend}, 122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * which is deprecated. Further, the results of this call 122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * were never well-defined. 122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Deprecated 1226a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public int countStackFrames() { 1227a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return getStackTrace().length; 1228a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Waits at most {@code millis} milliseconds for this thread to 123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * die. A timeout of {@code 0} means to wait forever. 123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> This implementation uses a loop of {@code this.wait} calls 123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conditioned on {@code this.isAlive}. As a thread terminates the 123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code this.notifyAll} method is invoked. It is recommended that 123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * applications not use {@code wait}, {@code notify}, or 123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code notifyAll} on {@code Thread} instances. 123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param millis 124151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the time to wait in milliseconds 124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the value of {@code millis} is negative 124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws InterruptedException 124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any thread has interrupted the current thread. The 124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>interrupted status</i> of the current thread is 124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * cleared when this exception is thrown. 125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1251a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public final void join(long millis) throws InterruptedException { 1252a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized(lock) { 125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long base = System.currentTimeMillis(); 125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long now = 0; 125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (millis < 0) { 125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException("timeout value is negative"); 125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (millis == 0) { 126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (isAlive()) { 1262a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski lock.wait(0); 126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (isAlive()) { 126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long delay = millis - now; 126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (delay <= 0) { 126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski break; 126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1270a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski lock.wait(delay); 127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski now = System.currentTimeMillis() - base; 127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1274a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Waits at most {@code millis} milliseconds plus 127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code nanos} nanoseconds for this thread to die. 128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> This implementation uses a loop of {@code this.wait} calls 128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * conditioned on {@code this.isAlive}. As a thread terminates the 128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code this.notifyAll} method is invoked. It is recommended that 128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * applications not use {@code wait}, {@code notify}, or 128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code notifyAll} on {@code Thread} instances. 128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param millis 128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the time to wait in milliseconds 128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param nanos 129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0-999999} additional nanoseconds to wait 129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the value of {@code millis} is negative, or the value 129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of {@code nanos} is not in the range {@code 0-999999} 129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws InterruptedException 129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any thread has interrupted the current thread. The 129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>interrupted status</i> of the current thread is 130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * cleared when this exception is thrown. 130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 1302a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski public final void join(long millis, int nanos) 130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws InterruptedException { 1304a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized(lock) { 130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (millis < 0) { 130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException("timeout value is negative"); 130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (nanos < 0 || nanos > 999999) { 131051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException( 131151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski "nanosecond timeout value out of range"); 131251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (nanos >= 500000 || (nanos != 0 && millis == 0)) { 131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski millis++; 131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski join(millis); 1319a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Waits for this thread to die. 132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> An invocation of this method behaves in exactly the same 132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * way as the invocation 132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote> 132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain #join(long) join}{@code (0)} 133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </blockquote> 133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws InterruptedException 133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if any thread has interrupted the current thread. The 133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>interrupted status</i> of the current thread is 133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * cleared when this exception is thrown. 133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void join() throws InterruptedException { 133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski join(0); 133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Prints a stack trace of the current thread to the standard error stream. 134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method is used only for debugging. 134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Throwable#printStackTrace() 134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void dumpStack() { 134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski new Exception("Stack trace").printStackTrace(); 134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Marks this thread as either a {@linkplain #isDaemon daemon} thread 135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or a user thread. The Java Virtual Machine exits when the only 135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * threads running are all daemon threads. 135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 135651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> This method must be invoked before the thread is started. 135751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 135851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param on 135951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code true}, marks this thread as a daemon thread 136051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 136151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalThreadStateException 136251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if this thread is {@linkplain #isAlive alive} 136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@link #checkAccess} determines that the current 136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread cannot modify this thread 136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void setDaemon(boolean on) { 136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski checkAccess(); 137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (isAlive()) { 137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalThreadStateException(); 137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski daemon = on; 137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Tests if this thread is a daemon thread. 137851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <code>true</code> if this thread is a daemon thread; 138051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>false</code> otherwise. 138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setDaemon(boolean) 138251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final boolean isDaemon() { 138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return daemon; 138551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 138851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Determines if the currently running thread has permission to 138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * modify this thread. 139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 139151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If there is a security manager, its <code>checkAccess</code> method 139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is called with this thread as its argument. This may result in 139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * throwing a <code>SecurityException</code>. 139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException if the current thread is not allowed to 139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * access this thread. 139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkAccess(Thread) 139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void checkAccess() { 140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of this thread, including the 140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * thread's name, priority, and thread group. 140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of this thread. 140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String toString() { 140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ThreadGroup group = getThreadGroup(); 141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (group != null) { 141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "Thread[" + getName() + "," + getPriority() + "," + 141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski group.getName() + "]"; 141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "Thread[" + getName() + "," + getPriority() + "," + 141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski "" + "]"; 141651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 141751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 141851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 141951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 142051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the context ClassLoader for this Thread. The context 142151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader is provided by the creator of the thread for use 142251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by code running in this thread when loading classes and resources. 142351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If not {@linkplain #setContextClassLoader set}, the default is the 142451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader context of the parent Thread. The context ClassLoader of the 142551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * primordial thread is typically set to the class loader used to load the 142651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * application. 142751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If a security manager is present, and the invoker's class loader is not 142951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} and is not the same as or an ancestor of the context class 143051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * loader, then this method invokes the security manager's {@link 143151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager#checkPermission(java.security.Permission) checkPermission} 143251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method with a {@link RuntimePermission RuntimePermission}{@code 143351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ("getClassLoader")} permission to verify that retrieval of the context 143451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class loader is permitted. 143551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 143651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the context ClassLoader for this Thread, or {@code null} 143751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * indicating the system class loader (or, failing that, the 143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * bootstrap class loader) 143951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 144151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the current thread cannot get the context ClassLoader 144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 144351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 144451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 144551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @CallerSensitive 144651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public ClassLoader getContextClassLoader() { 144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return contextClassLoader; 144851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 145051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 145151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the context ClassLoader for this Thread. The context 145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader can be set when a thread is created, and allows 145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the creator of the thread to provide the appropriate class loader, 145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * through {@code getContextClassLoader}, to code running in the thread 145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * when loading classes and resources. 145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If a security manager is present, its {@link 145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * SecurityManager#checkPermission(java.security.Permission) checkPermission} 145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is invoked with a {@link RuntimePermission RuntimePermission}{@code 146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ("setContextClassLoader")} permission to see if setting the context 146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ClassLoader is permitted. 146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param cl 146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the context ClassLoader for this Thread, or null indicating the 146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * system class loader (or, failing that, the bootstrap class loader) 146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 146751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the current thread cannot set the context ClassLoader 146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 147251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public void setContextClassLoader(ClassLoader cl) { 147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski contextClassLoader = cl; 147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if and only if the current thread holds the 147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * monitor lock on the specified object. 147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method is designed to allow a program to assert that 148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current thread already holds a specified lock: 148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> 148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * assert Thread.holdsLock(obj); 148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre> 148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param obj the object on which to test lock ownership 148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if obj is <tt>null</tt> 148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the current thread holds the monitor lock on 148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified object. 149051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 149151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 149246c38b30477d579beaa528386f71814c0191a48fNarayan Kamath public static boolean holdsLock(Object obj) { 149346c38b30477d579beaa528386f71814c0191a48fNarayan Kamath return currentThread().nativeHoldsLock(obj); 1494a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 1495a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1496a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native boolean nativeHoldsLock(Object object); 149751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 149851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final StackTraceElement[] EMPTY_STACK_TRACE 149951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski = new StackTraceElement[0]; 150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 150251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns an array of stack trace elements representing the stack dump 150351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of this thread. This method will return a zero-length array if 150451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this thread has not started, has started but has not yet been 150551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * scheduled to run by the system, or has terminated. 150651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the returned array is of non-zero length then the first element of 150751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array represents the top of the stack, which is the most recent 150851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method invocation in the sequence. The last element of the array 150951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * represents the bottom of the stack, which is the least recent method 151051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * invocation in the sequence. 151151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 151251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If there is a security manager, and this thread is not 151351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current thread, then the security manager's 151451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>checkPermission</tt> method is called with a 151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>RuntimePermission("getStackTrace")</tt> permission 151651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to see if it's ok to get the stack trace. 151751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 151851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Some virtual machines may, under some circumstances, omit one 151951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or more stack frames from the stack trace. In the extreme case, 152051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a virtual machine that has no stack trace information concerning 152151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this thread is permitted to return a zero-length array from this 152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method. 152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return an array of <tt>StackTraceElement</tt>, 152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * each represents one stack frame. 152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>checkPermission</tt> method doesn't allow 153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * getting the stack trace of thread. 153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see RuntimePermission 153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Throwable#getStackTrace 153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 153551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public StackTraceElement[] getStackTrace() { 1538a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski StackTraceElement ste[] = VMStack.getThreadStackTrace(this); 1539a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return ste != null ? ste : EmptyArray.STACK_TRACE_ELEMENT; 154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a map of stack traces for all live threads. 154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The map keys are threads and each map value is an array of 154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>StackTraceElement</tt> that represents the stack dump 154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the corresponding <tt>Thread</tt>. 154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The returned stack traces are in the format specified for 154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the {@link #getStackTrace getStackTrace} method. 154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The threads may be executing while this method is called. 155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The stack trace of each thread only represents a snapshot and 155251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * each stack trace may be obtained at different time. A zero-length 155351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array will be returned in the map value if the virtual machine has 155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * no stack trace information about a thread. 155551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If there is a security manager, then the security manager's 155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>checkPermission</tt> method is called with a 155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>RuntimePermission("getStackTrace")</tt> permission as well as 155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>RuntimePermission("modifyThreadGroup")</tt> permission 156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to see if it is ok to get the stack trace of all threads. 156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a <tt>Map</tt> from <tt>Thread</tt> to an array of 156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>StackTraceElement</tt> that represents the stack trace of 156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the corresponding thread. 156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException 156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if a security manager exists and its 156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>checkPermission</tt> method doesn't allow 156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * getting the stack trace of thread. 157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #getStackTrace 157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see SecurityManager#checkPermission 157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see RuntimePermission 157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Throwable#getStackTrace 157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 157651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 157751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static Map<Thread, StackTraceElement[]> getAllStackTraces() { 1578a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Map<Thread, StackTraceElement[]> map = new HashMap<Thread, StackTraceElement[]>(); 157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 1580a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Find out how many live threads we have. Allocate a bit more 1581a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // space than needed, in case new ones are just being created. 1582a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski int count = ThreadGroup.systemThreadGroup.activeCount(); 1583a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski Thread[] threads = new Thread[count + count / 2]; 1584a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1585a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski // Enumerate the threads and collect the stacktraces. 1586a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski count = ThreadGroup.systemThreadGroup.enumerate(threads); 1587a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski for (int i = 0; i < count; i++) { 1588a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski map.put(threads[i], threads[i].getStackTrace()); 158951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 1590a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 1591a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return map; 159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 159351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 159451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 159551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final RuntimePermission SUBCLASS_IMPLEMENTATION_PERMISSION = 159651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski new RuntimePermission("enableContextClassLoaderOverride"); 159751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 159851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** cache of subclass security audit results */ 159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Replace with ConcurrentReferenceHashMap when/if it appears in a future 160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * release */ 160151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static class Caches { 160251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** cache of subclass security audit results */ 160351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits = 160451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski new ConcurrentHashMap<>(); 160551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 160651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** queue for WeakReferences to audited subclasses */ 160751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static final ReferenceQueue<Class<?>> subclassAuditsQueue = 160851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski new ReferenceQueue<>(); 160951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 161051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 161151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 161251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Verifies that this (possibly subclass) instance can be constructed 161351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * without violating security constraints: the subclass must not override 161451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * security-sensitive non-final methods, or else the 161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * "enableContextClassLoaderOverride" RuntimePermission is checked. 161651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 16174e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak private static boolean isCCLOverridden(Class<?> cl) { 161851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cl == Thread.class) 161951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski processQueue(Caches.subclassAuditsQueue, Caches.subclassAudits); 162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue); 162351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Boolean result = Caches.subclassAudits.get(key); 162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (result == null) { 162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = Boolean.valueOf(auditSubclass(cl)); 162651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Caches.subclassAudits.putIfAbsent(key, result); 162751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 162851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 162951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result.booleanValue(); 163051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 163151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 163251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 163351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Performs reflective checks on given subclass to verify that it doesn't 163451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * override security-sensitive non-final methods. Returns true if the 163551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subclass overrides any of the methods, false otherwise. 163651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 16374e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak private static boolean auditSubclass(final Class<?> subcl) { 163851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Boolean result = AccessController.doPrivileged( 163951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski new PrivilegedAction<Boolean>() { 164051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Boolean run() { 16414e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak for (Class<?> cl = subcl; 164251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski cl != Thread.class; 164351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski cl = cl.getSuperclass()) 164451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 164551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 16464e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak cl.getDeclaredMethod("getContextClassLoader", new Class<?>[0]); 164751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Boolean.TRUE; 164851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } catch (NoSuchMethodException ex) { 164951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski try { 16514e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak Class<?>[] params = {ClassLoader.class}; 165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski cl.getDeclaredMethod("setContextClassLoader", params); 165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Boolean.TRUE; 165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } catch (NoSuchMethodException ex) { 165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Boolean.FALSE; 165851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 165951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 166051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ); 166151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result.booleanValue(); 166251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 166351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 166451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 166551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the identifier of this Thread. The thread ID is a positive 166651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>long</tt> number generated when this thread was created. 166751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The thread ID is unique and remains unchanged during its lifetime. 166851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * When a thread is terminated, this thread ID may be reused. 166951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 167051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this thread's ID. 167151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 167251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 167351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public long getId() { 167451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return tid; 167551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 167651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 167751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 167851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread state. A thread can be in one of the following states: 167951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 168051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link #NEW}<br> 168151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread that has not yet started is in this state. 168251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </li> 168351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link #RUNNABLE}<br> 168451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread executing in the Java virtual machine is in this state. 168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </li> 168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link #BLOCKED}<br> 168751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread that is blocked waiting for a monitor lock 168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is in this state. 168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </li> 169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link #WAITING}<br> 169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread that is waiting indefinitely for another thread to 169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * perform a particular action is in this state. 169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </li> 169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link #TIMED_WAITING}<br> 169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread that is waiting for another thread to perform an action 169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for up to a specified waiting time is in this state. 169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </li> 169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link #TERMINATED}<br> 169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread that has exited is in this state. 170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </li> 170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread can be in only one state at a given point in time. 170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * These states are virtual machine states which do not reflect 170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * any operating system thread states. 170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #getState 171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 171151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public enum State { 171251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 171351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Thread state for a thread which has not yet started. 171451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 171551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski NEW, 171651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Thread state for a runnable thread. A thread in the runnable 171951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * state is executing in the Java virtual machine but it may 172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be waiting for other resources from the operating system 172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such as processor. 172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski RUNNABLE, 172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 172551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Thread state for a thread blocked waiting for a monitor lock. 172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread in the blocked state is waiting for a monitor lock 172851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to enter a synchronized block/method or 172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reenter a synchronized block/method after calling 173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Object#wait() Object.wait}. 173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski BLOCKED, 173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Thread state for a waiting thread. 173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread is in the waiting state due to calling one of the 173751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following methods: 173851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 173951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link Object#wait() Object.wait} with no timeout</li> 174051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link #join() Thread.join} with no timeout</li> 174151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link LockSupport#park() LockSupport.park}</li> 174251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 174351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 174451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>A thread in the waiting state is waiting for another thread to 174551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * perform a particular action. 174651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 174751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For example, a thread that has called <tt>Object.wait()</tt> 174851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * on an object is waiting for another thread to call 174951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on 175051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that object. A thread that has called <tt>Thread.join()</tt> 175151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is waiting for a specified thread to terminate. 175251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 175351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski WAITING, 175451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 175551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 175651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Thread state for a waiting thread with a specified waiting time. 175751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A thread is in the timed waiting state due to calling one of 175851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the following methods with a specified positive waiting time: 175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 176051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link #sleep Thread.sleep}</li> 176151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link Object#wait(long) Object.wait} with timeout</li> 176251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link #join(long) Thread.join} with timeout</li> 176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> 176451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> 176551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 176651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski TIMED_WAITING, 176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Thread state for a terminated thread. 177151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The thread has completed execution. 177251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski TERMINATED; 177451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 177551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 177651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 177751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the state of this thread. 177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method is designed for use in monitoring of the system state, 177951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not for synchronization control. 178051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 178151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return this thread's state. 178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 178351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public State getState() { 178551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // get current thread state 1786a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski return State.values()[nativeGetStatus(started)]; 178751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 178851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 178951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Added in JSR-166 179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 179151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 179251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Interface for handlers invoked when a <tt>Thread</tt> abruptly 179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * terminates due to an uncaught exception. 179451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>When a thread is about to terminate due to an uncaught exception 179551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the Java Virtual Machine will query the thread for its 179651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>UncaughtExceptionHandler</tt> using 179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #getUncaughtExceptionHandler} and will invoke the handler's 179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>uncaughtException</tt> method, passing the thread and the 179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exception as arguments. 180051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If a thread has not had its <tt>UncaughtExceptionHandler</tt> 180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * explicitly set, then its <tt>ThreadGroup</tt> object acts as its 180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>UncaughtExceptionHandler</tt>. If the <tt>ThreadGroup</tt> object 180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has no 180451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * special requirements for dealing with the exception, it can forward 180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the invocation to the {@linkplain #getDefaultUncaughtExceptionHandler 180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * default uncaught exception handler}. 180751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 180851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setDefaultUncaughtExceptionHandler 180951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setUncaughtExceptionHandler 181051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see ThreadGroup#uncaughtException 181151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 181251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 18134e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak @FunctionalInterface 181451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public interface UncaughtExceptionHandler { 181551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 181651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Method invoked when the given thread terminates due to the 181751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * given uncaught exception. 181851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Any exception thrown by this method will be ignored by the 181951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Virtual Machine. 182051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param t the thread 182151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param e the exception 182251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 182351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski void uncaughtException(Thread t, Throwable e); 182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 182551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 182651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // null unless explicitly set 182751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private volatile UncaughtExceptionHandler uncaughtExceptionHandler; 182851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // null unless explicitly set 183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler; 183151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 183251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Set the default handler invoked when a thread abruptly terminates 183451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * due to an uncaught exception, and no other handler has been defined 183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for that thread. 183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Uncaught exception handling is controlled first by the thread, then 183851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the thread's {@link ThreadGroup} object and finally by the default 183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * uncaught exception handler. If the thread does not have an explicit 184051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * uncaught exception handler set, and the thread's thread group 184151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (including parent thread groups) does not specialize its 184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>uncaughtException</tt> method, then the default handler's 184351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>uncaughtException</tt> method will be invoked. 184451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>By setting the default uncaught exception handler, an application 184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * can change the way in which uncaught exceptions are handled (such as 184651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * logging to a specific device, or file) for those threads that would 184751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * already accept whatever "default" behavior the system 184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * provided. 184951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 185051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that the default uncaught exception handler should not usually 185151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * defer to the thread's <tt>ThreadGroup</tt> object, as that could cause 185251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * infinite recursion. 185351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 185451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param eh the object to use as the default uncaught exception handler. 185551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>null</tt> then there is no default handler. 185651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 185751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException if a security manager is present and it 185851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * denies <tt>{@link RuntimePermission} 185951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ("setDefaultUncaughtExceptionHandler")</tt> 186051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setUncaughtExceptionHandler 186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #getUncaughtExceptionHandler 186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see ThreadGroup#uncaughtException 186451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) { 186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski defaultUncaughtExceptionHandler = eh; 186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 187151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the default handler invoked when a thread abruptly terminates 187251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * due to an uncaught exception. If the returned value is <tt>null</tt>, 187351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * there is no default. 187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setDefaultUncaughtExceptionHandler 18764e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * @return the default uncaught exception handler for all threads 187751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 187851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){ 187951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return defaultUncaughtExceptionHandler; 188051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 188151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 1882b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer // Android-changed: Added concept of an uncaughtExceptionPreHandler for use by platform. 1883b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer // null unless explicitly set 1884b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer private static volatile UncaughtExceptionHandler uncaughtExceptionPreHandler; 1885b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer 1886b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer /** 1887b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * Sets an {@link UncaughtExceptionHandler} that will be called before any 1888b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * returned by {@link #getUncaughtExceptionHandler()}. To allow the standard 1889b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * handlers to run, this handler should never terminate this process. Any 1890b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * throwables thrown by the handler will be ignored by 1891b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * {@link #dispatchUncaughtException(Throwable)}. 1892b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * 1893b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * @hide only for use by the Android framework (RuntimeInit) b/29624607 1894b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer */ 1895b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer public static void setUncaughtExceptionPreHandler(UncaughtExceptionHandler eh) { 1896b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer uncaughtExceptionPreHandler = eh; 1897b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer } 1898b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer 1899b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer /** @hide */ 1900b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer public static UncaughtExceptionHandler getUncaughtExceptionPreHandler() { 1901b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer return uncaughtExceptionPreHandler; 1902b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer } 1903b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer 190451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 190551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the handler invoked when this thread abruptly terminates 190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * due to an uncaught exception. If this thread has not had an 190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * uncaught exception handler explicitly set then this thread's 190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>ThreadGroup</tt> object is returned, unless this thread 190951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * has terminated, in which case <tt>null</tt> is returned. 191051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 19114e73fd2bc22e704fe71465782b83be0471ac5e82Przemyslaw Szczepaniak * @return the uncaught exception handler for this thread 191251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 191351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public UncaughtExceptionHandler getUncaughtExceptionHandler() { 191451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return uncaughtExceptionHandler != null ? 191551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski uncaughtExceptionHandler : group; 191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 191751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 191851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 191951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Set the handler invoked when this thread abruptly terminates 192051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * due to an uncaught exception. 192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>A thread can take full control of how it responds to uncaught 192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exceptions by having its uncaught exception handler explicitly set. 192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If no such handler is set then the thread's <tt>ThreadGroup</tt> 192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object acts as its handler. 192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param eh the object to use as this thread's uncaught exception 192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * handler. If <tt>null</tt> then this thread has no explicit handler. 192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws SecurityException if the current thread is not allowed to 192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * modify this thread. 192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #setDefaultUncaughtExceptionHandler 193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see ThreadGroup#uncaughtException 193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) { 193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski checkAccess(); 193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski uncaughtExceptionHandler = eh; 193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 193851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Dispatch an uncaught exception to the handler. This method is 1940b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * intended to be called only by the runtime and by tests. 1941b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * 1942b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer * @hide 194351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 194459614472b1f367c9f374ec5f44ff44dfd7409e39Narayan Kamath // @VisibleForTesting (would be package-private if not for tests) 1945b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer public final void dispatchUncaughtException(Throwable e) { 1946b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer Thread.UncaughtExceptionHandler initialUeh = 1947b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer Thread.getUncaughtExceptionPreHandler(); 1948b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer if (initialUeh != null) { 1949b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer try { 1950b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer initialUeh.uncaughtException(this, e); 1951b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer } catch (RuntimeException | Error ignored) { 1952b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer // Throwables thrown by the initial handler are ignored 1953b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer } 1954b62710c762bf399ee1a45821eb0740b2d9c92f62Tobias Thierer } 195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski getUncaughtExceptionHandler().uncaughtException(this, e); 195651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 195751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 195851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 195951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Removes from the specified map any keys that have been enqueued 196051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * on the specified reference queue. 196151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 196251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static void processQueue(ReferenceQueue<Class<?>> queue, 196351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ConcurrentMap<? extends 196451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski WeakReference<Class<?>>, ?> map) 196551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 196651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Reference<? extends Class<?>> ref; 196751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while((ref = queue.poll()) != null) { 196851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski map.remove(ref); 196951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 197051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 197151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 197251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 197351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Weak key for Class objects. 197451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski **/ 197551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static class WeakClassKey extends WeakReference<Class<?>> { 197651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * saved value of the referent's identity hash code, to maintain 197851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a consistent hash code after the referent has been cleared 197951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private final int hash; 198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 198351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Create a new WeakClassKey to the given object, registered 198451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with a queue. 198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski WeakClassKey(Class<?> cl, ReferenceQueue<Class<?>> refQueue) { 198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski super(cl, refQueue); 198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski hash = System.identityHashCode(cl); 198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the identity hash code of the original referent. 199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Override 199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int hashCode() { 199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return hash; 199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 200051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if the given object is this identical 200151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * WeakClassKey instance, or, if this object's referent has not 200251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * been cleared, if the given object is another WeakClassKey 200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instance with the identical non-null referent as this one. 200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 200551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @Override 200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean equals(Object obj) { 200751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (obj == this) 200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 200951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 201051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (obj instanceof WeakClassKey) { 201151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object referent = get(); 201251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (referent != null) && 201351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski (referent == ((WeakClassKey) obj).get()); 201451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 201551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 201651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 201751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 201851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 201951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2020b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak 2021b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak // The following three initially uninitialized fields are exclusively 2022b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak // managed by class java.util.concurrent.ThreadLocalRandom. These 2023b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak // fields are used to build the high-performance PRNGs in the 2024b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak // concurrent code, and we can not risk accidental false sharing. 2025b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak // Hence, the fields are isolated with @Contended. 2026b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak 2027b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak /** The current seed for a ThreadLocalRandom */ 2028b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak // @sun.misc.Contended("tlr") 2029b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak long threadLocalRandomSeed; 2030b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak 2031b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak /** Probe hash value; nonzero if threadLocalRandomSeed initialized */ 2032b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak // @sun.misc.Contended("tlr") 2033b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak int threadLocalRandomProbe; 2034b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak 2035b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak /** Secondary seed isolated from public ThreadLocalRandom sequence */ 2036b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak // @sun.misc.Contended("tlr") 2037b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak int threadLocalRandomSecondarySeed; 2038b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak 203951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* Some private helper methods */ 2040a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native void nativeSetName(String newName); 2041a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2042a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native void nativeSetPriority(int newPriority); 2043a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2044a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native int nativeGetStatus(boolean hasBeenStarted); 2045a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2046fa5b565a3f6c6d7cbd6106ee8d360304c3a939a3Igor Murashkin @FastNative 2047a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private native void nativeInterrupt(); 2048a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2049a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** Park states */ 2050a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static class ParkState { 2051a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** park state indicating unparked */ 2052a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static final int UNPARKED = 1; 2053a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2054a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** park state indicating preemptively unparked */ 2055a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static final int PREEMPTIVELY_UNPARKED = 2; 2056a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2057a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** park state indicating parked */ 2058a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static final int PARKED = 3; 2059a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2060a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2061a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private static final int NANOS_PER_MILLI = 1000000; 2062a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2063a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** the park state of the thread */ 2064a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski private int parkState = ParkState.UNPARKED; 2065a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2066a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2067a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Unparks this thread. This unblocks the thread it if it was 2068a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * previously parked, or indicates that the thread is "preemptively 2069a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * unparked" if it wasn't already parked. The latter means that the 2070a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * next time the thread is told to park, it will merely clear its 2071a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * latent park bit and carry on without blocking. 2072a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2073a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * <p>See {@link java.util.concurrent.locks.LockSupport} for more 2074a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * in-depth information of the behavior of this method.</p> 2075a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2076a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide for Unsafe 2077a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2078232ed175b796126887c9a9517bc57b76fcb05318Przemyslaw Szczepaniak public final void unpark$() { 2079a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized(lock) { 2080a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski switch (parkState) { 2081a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski case ParkState.PREEMPTIVELY_UNPARKED: { 2082a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /* 2083a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Nothing to do in this case: By definition, a 2084a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * preemptively unparked thread is to remain in 2085a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * the preemptively unparked state if it is told 2086a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * to unpark. 2087a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2088a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski break; 2089a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2090a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski case ParkState.UNPARKED: { 2091a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parkState = ParkState.PREEMPTIVELY_UNPARKED; 2092a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski break; 2093a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2094a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski default /*parked*/: { 2095a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parkState = ParkState.UNPARKED; 2096a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski lock.notifyAll(); 2097a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski break; 2098a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2099a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2100a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2101a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2102a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2103a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2104a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Parks the current thread for a particular number of nanoseconds, or 2105a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * indefinitely. If not indefinitely, this method unparks the thread 2106a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * after the given number of nanoseconds if no other thread unparks it 2107a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * first. If the thread has been "preemptively unparked," this method 2108a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * cancels that unparking and returns immediately. This method may 2109a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * also return spuriously (that is, without the thread being told to 2110a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * unpark and without the indicated amount of time elapsing). 2111a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2112a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * <p>See {@link java.util.concurrent.locks.LockSupport} for more 2113a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * in-depth information of the behavior of this method.</p> 2114a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2115a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * <p>This method must only be called when <code>this</code> is the current 2116a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * thread. 2117a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2118a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @param nanos number of nanoseconds to park for or <code>0</code> 2119a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * to park indefinitely 2120a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @throws IllegalArgumentException thrown if <code>nanos < 0</code> 2121a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2122a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide for Unsafe 2123a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2124232ed175b796126887c9a9517bc57b76fcb05318Przemyslaw Szczepaniak public final void parkFor$(long nanos) { 2125a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized(lock) { 2126a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski switch (parkState) { 2127a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski case ParkState.PREEMPTIVELY_UNPARKED: { 2128a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parkState = ParkState.UNPARKED; 2129a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski break; 2130a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2131a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski case ParkState.UNPARKED: { 2132a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski long millis = nanos / NANOS_PER_MILLI; 2133a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski nanos %= NANOS_PER_MILLI; 2134a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2135a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parkState = ParkState.PARKED; 2136a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski try { 2137a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski lock.wait(millis, (int) nanos); 2138a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } catch (InterruptedException ex) { 2139a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski interrupt(); 2140a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } finally { 2141a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /* 2142a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Note: If parkState manages to become 2143a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * PREEMPTIVELY_UNPARKED before hitting this 2144a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * code, it should left in that state. 2145a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2146a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski if (parkState == ParkState.PARKED) { 2147a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parkState = ParkState.UNPARKED; 2148a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2149a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2150a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski break; 2151a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2152a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski default /*parked*/: { 2153a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski throw new AssertionError("Attempt to repark"); 2154a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2155a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2156a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2157a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2158a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski 2159a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /** 2160a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Parks the current thread until the specified system time. This 2161a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * method attempts to unpark the current thread immediately after 2162a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * <code>System.currentTimeMillis()</code> reaches the specified 2163a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * value, if no other thread unparks it first. If the thread has 2164a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * been "preemptively unparked," this method cancels that 2165a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * unparking and returns immediately. This method may also return 2166a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * spuriously (that is, without the thread being told to unpark 2167a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * and without the indicated amount of time elapsing). 2168a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2169a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * <p>See {@link java.util.concurrent.locks.LockSupport} for more 2170a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * in-depth information of the behavior of this method.</p> 2171a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2172a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * <p>This method must only be called when <code>this</code> is the 2173a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * current thread. 2174a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2175a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @param time the time after which the thread should be unparked, 2176a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * in absolute milliseconds-since-the-epoch 2177a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * 2178a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * @hide for Unsafe 2179a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2180232ed175b796126887c9a9517bc57b76fcb05318Przemyslaw Szczepaniak public final void parkUntil$(long time) { 2181a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski synchronized(lock) { 2182a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski /* 2183a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * Note: This conflates the two time bases of "wall clock" 2184a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * time and "monotonic uptime" time. However, given that 2185a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * the underlying system can only wait on monotonic time, 2186a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * it is unclear if there is any way to avoid the 2187a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * conflation. The downside here is that if, having 2188a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * calculated the delay, the wall clock gets moved ahead, 2189a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * this method may not return until well after the wall 2190a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * clock has reached the originally designated time. The 2191a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * reverse problem (the wall clock being turned back) 2192a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * isn't a big deal, since this method is allowed to 2193a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * spuriously return for any reason, and this situation 2194a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski * can safely be construed as just such a spurious return. 2195a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski */ 2196bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak final long currentTime = System.currentTimeMillis(); 2197bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak if (time <= currentTime) { 2198a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski parkState = ParkState.UNPARKED; 2199a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } else { 2200bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak long delayMillis = time - currentTime; 2201bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak // Long.MAX_VALUE / NANOS_PER_MILLI (0x8637BD05SF6) is the largest 2202bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak // long value that won't overflow to negative value when 2203bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak // multiplyed by NANOS_PER_MILLI (10^6). 2204bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak long maxValue = (Long.MAX_VALUE / NANOS_PER_MILLI); 2205bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak if (delayMillis > maxValue) { 2206bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak delayMillis = maxValue; 2207bf34d8378762aa4d97c30c86ef95502e5d85ed3ePrzemyslaw Szczepaniak } 2208232ed175b796126887c9a9517bc57b76fcb05318Przemyslaw Szczepaniak parkFor$(delayMillis * NANOS_PER_MILLI); 2209a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2210a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 2211a80f9aeb7b637fc241c75448eed08275245ec652Piotr Jastrzebski } 221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 2213