< prev index next >

src/java.base/share/classes/java/lang/Thread.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.ref.Reference;
  29 import java.lang.ref.ReferenceQueue;
  30 import java.lang.ref.WeakReference;
  31 import java.security.AccessController;
  32 import java.security.AccessControlContext;
  33 import java.security.PrivilegedAction;
  34 import java.util.Map;
  35 import java.util.HashMap;
  36 import java.util.concurrent.ConcurrentHashMap;
  37 import java.util.concurrent.ConcurrentMap;
  38 import java.util.concurrent.locks.LockSupport;


  39 import sun.nio.ch.Interruptible;
  40 import jdk.internal.reflect.CallerSensitive;
  41 import jdk.internal.reflect.Reflection;
  42 import sun.security.util.SecurityConstants;
  43 import jdk.internal.HotSpotIntrinsicCandidate;
  44 
  45 /**
  46  * A <i>thread</i> is a thread of execution in a program. The Java
  47  * Virtual Machine allows an application to have multiple threads of
  48  * execution running concurrently.
  49  * <p>
  50  * Every thread has a priority. Threads with higher priority are
  51  * executed in preference to threads with lower priority. Each thread
  52  * may or may not also be marked as a daemon. When code running in
  53  * some thread creates a new {@code Thread} object, the new
  54  * thread has its priority initially set equal to the priority of the
  55  * creating thread, and is a daemon thread if and only if the
  56  * creating thread is a daemon.
  57  * <p>
  58  * When a Java Virtual Machine starts up, there is usually a single


 821      * otherwise, this method does nothing and returns.
 822      * <p>
 823      * Subclasses of {@code Thread} should override this method.
 824      *
 825      * @see     #start()
 826      * @see     #stop()
 827      * @see     #Thread(ThreadGroup, Runnable, String)
 828      */
 829     @Override
 830     public void run() {
 831         if (target != null) {
 832             target.run();
 833         }
 834     }
 835 
 836     /**
 837      * This method is called by the system to give a Thread
 838      * a chance to clean up before it actually exits.
 839      */
 840     private void exit() {

 841         if (group != null) {
 842             group.threadTerminated(this);
 843             group = null;
 844         }
 845         /* Aggressively null out all reference fields: see bug 4006245 */
 846         target = null;
 847         /* Speed the release of some of these resources */
 848         threadLocals = null;
 849         inheritableThreadLocals = null;
 850         inheritedAccessControlContext = null;
 851         blocker = null;
 852         uncaughtExceptionHandler = null;
 853     }
 854 
 855     /**
 856      * Forces the thread to stop executing.
 857      * <p>
 858      * If there is a security manager installed, its {@code checkAccess}
 859      * method is called with {@code this}
 860      * as its argument. This may result in a




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.ref.Reference;
  29 import java.lang.ref.ReferenceQueue;
  30 import java.lang.ref.WeakReference;
  31 import java.security.AccessController;
  32 import java.security.AccessControlContext;
  33 import java.security.PrivilegedAction;
  34 import java.util.Map;
  35 import java.util.HashMap;
  36 import java.util.concurrent.ConcurrentHashMap;
  37 import java.util.concurrent.ConcurrentMap;
  38 import java.util.concurrent.locks.LockSupport;
  39 
  40 import jdk.internal.misc.JdkThreadLocal;
  41 import sun.nio.ch.Interruptible;
  42 import jdk.internal.reflect.CallerSensitive;
  43 import jdk.internal.reflect.Reflection;
  44 import sun.security.util.SecurityConstants;
  45 import jdk.internal.HotSpotIntrinsicCandidate;
  46 
  47 /**
  48  * A <i>thread</i> is a thread of execution in a program. The Java
  49  * Virtual Machine allows an application to have multiple threads of
  50  * execution running concurrently.
  51  * <p>
  52  * Every thread has a priority. Threads with higher priority are
  53  * executed in preference to threads with lower priority. Each thread
  54  * may or may not also be marked as a daemon. When code running in
  55  * some thread creates a new {@code Thread} object, the new
  56  * thread has its priority initially set equal to the priority of the
  57  * creating thread, and is a daemon thread if and only if the
  58  * creating thread is a daemon.
  59  * <p>
  60  * When a Java Virtual Machine starts up, there is usually a single


 823      * otherwise, this method does nothing and returns.
 824      * <p>
 825      * Subclasses of {@code Thread} should override this method.
 826      *
 827      * @see     #start()
 828      * @see     #stop()
 829      * @see     #Thread(ThreadGroup, Runnable, String)
 830      */
 831     @Override
 832     public void run() {
 833         if (target != null) {
 834             target.run();
 835         }
 836     }
 837 
 838     /**
 839      * This method is called by the system to give a Thread
 840      * a chance to clean up before it actually exits.
 841      */
 842     private void exit() {
 843         JdkThreadLocal.threadTerminated(JdkThreadLocal.REGISTRY.getIfPresent());
 844         if (group != null) {
 845             group.threadTerminated(this);
 846             group = null;
 847         }
 848         /* Aggressively null out all reference fields: see bug 4006245 */
 849         target = null;
 850         /* Speed the release of some of these resources */
 851         threadLocals = null;
 852         inheritableThreadLocals = null;
 853         inheritedAccessControlContext = null;
 854         blocker = null;
 855         uncaughtExceptionHandler = null;
 856     }
 857 
 858     /**
 859      * Forces the thread to stop executing.
 860      * <p>
 861      * If there is a security manager installed, its {@code checkAccess}
 862      * method is called with {@code this}
 863      * as its argument. This may result in a


< prev index next >