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