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