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