rt/emul/compact/src/main/java/java/lang/Thread.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 31 Oct 2013 15:16:56 +0100
changeset 1403 8120793cc756
parent 1260 fe3567c7b522
permissions -rw-r--r--
Allow idempotent setting of system classloader
jaroslav@1258
     1
/*
jaroslav@1258
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@1258
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1258
     4
 *
jaroslav@1258
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1258
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1258
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1258
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1258
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1258
    10
 *
jaroslav@1258
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1258
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1258
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1258
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1258
    15
 * accompanied this code).
jaroslav@1258
    16
 *
jaroslav@1258
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1258
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1258
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1258
    20
 *
jaroslav@1258
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1258
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1258
    23
 * questions.
jaroslav@1258
    24
 */
jaroslav@1258
    25
jaroslav@1258
    26
package java.lang;
jaroslav@1258
    27
jaroslav@1258
    28
import java.util.Map;
jaroslav@1258
    29
jaroslav@1258
    30
jaroslav@1258
    31
/**
jaroslav@1258
    32
 * A <i>thread</i> is a thread of execution in a program. The Java
jaroslav@1258
    33
 * Virtual Machine allows an application to have multiple threads of
jaroslav@1258
    34
 * execution running concurrently.
jaroslav@1258
    35
 * <p>
jaroslav@1258
    36
 * Every thread has a priority. Threads with higher priority are
jaroslav@1258
    37
 * executed in preference to threads with lower priority. Each thread
jaroslav@1258
    38
 * may or may not also be marked as a daemon. When code running in
jaroslav@1258
    39
 * some thread creates a new <code>Thread</code> object, the new
jaroslav@1258
    40
 * thread has its priority initially set equal to the priority of the
jaroslav@1258
    41
 * creating thread, and is a daemon thread if and only if the
jaroslav@1258
    42
 * creating thread is a daemon.
jaroslav@1258
    43
 * <p>
jaroslav@1258
    44
 * When a Java Virtual Machine starts up, there is usually a single
jaroslav@1258
    45
 * non-daemon thread (which typically calls the method named
jaroslav@1258
    46
 * <code>main</code> of some designated class). The Java Virtual
jaroslav@1258
    47
 * Machine continues to execute threads until either of the following
jaroslav@1258
    48
 * occurs:
jaroslav@1258
    49
 * <ul>
jaroslav@1258
    50
 * <li>The <code>exit</code> method of class <code>Runtime</code> has been
jaroslav@1258
    51
 *     called and the security manager has permitted the exit operation
jaroslav@1258
    52
 *     to take place.
jaroslav@1258
    53
 * <li>All threads that are not daemon threads have died, either by
jaroslav@1258
    54
 *     returning from the call to the <code>run</code> method or by
jaroslav@1258
    55
 *     throwing an exception that propagates beyond the <code>run</code>
jaroslav@1258
    56
 *     method.
jaroslav@1258
    57
 * </ul>
jaroslav@1258
    58
 * <p>
jaroslav@1258
    59
 * There are two ways to create a new thread of execution. One is to
jaroslav@1258
    60
 * declare a class to be a subclass of <code>Thread</code>. This
jaroslav@1258
    61
 * subclass should override the <code>run</code> method of class
jaroslav@1258
    62
 * <code>Thread</code>. An instance of the subclass can then be
jaroslav@1258
    63
 * allocated and started. For example, a thread that computes primes
jaroslav@1258
    64
 * larger than a stated value could be written as follows:
jaroslav@1258
    65
 * <p><hr><blockquote><pre>
jaroslav@1258
    66
 *     class PrimeThread extends Thread {
jaroslav@1258
    67
 *         long minPrime;
jaroslav@1258
    68
 *         PrimeThread(long minPrime) {
jaroslav@1258
    69
 *             this.minPrime = minPrime;
jaroslav@1258
    70
 *         }
jaroslav@1258
    71
 *
jaroslav@1258
    72
 *         public void run() {
jaroslav@1258
    73
 *             // compute primes larger than minPrime
jaroslav@1258
    74
 *             &nbsp;.&nbsp;.&nbsp;.
jaroslav@1258
    75
 *         }
jaroslav@1258
    76
 *     }
jaroslav@1258
    77
 * </pre></blockquote><hr>
jaroslav@1258
    78
 * <p>
jaroslav@1258
    79
 * The following code would then create a thread and start it running:
jaroslav@1258
    80
 * <p><blockquote><pre>
jaroslav@1258
    81
 *     PrimeThread p = new PrimeThread(143);
jaroslav@1258
    82
 *     p.start();
jaroslav@1258
    83
 * </pre></blockquote>
jaroslav@1258
    84
 * <p>
jaroslav@1258
    85
 * The other way to create a thread is to declare a class that
jaroslav@1258
    86
 * implements the <code>Runnable</code> interface. That class then
jaroslav@1258
    87
 * implements the <code>run</code> method. An instance of the class can
jaroslav@1258
    88
 * then be allocated, passed as an argument when creating
jaroslav@1258
    89
 * <code>Thread</code>, and started. The same example in this other
jaroslav@1258
    90
 * style looks like the following:
jaroslav@1258
    91
 * <p><hr><blockquote><pre>
jaroslav@1258
    92
 *     class PrimeRun implements Runnable {
jaroslav@1258
    93
 *         long minPrime;
jaroslav@1258
    94
 *         PrimeRun(long minPrime) {
jaroslav@1258
    95
 *             this.minPrime = minPrime;
jaroslav@1258
    96
 *         }
jaroslav@1258
    97
 *
jaroslav@1258
    98
 *         public void run() {
jaroslav@1258
    99
 *             // compute primes larger than minPrime
jaroslav@1258
   100
 *             &nbsp;.&nbsp;.&nbsp;.
jaroslav@1258
   101
 *         }
jaroslav@1258
   102
 *     }
jaroslav@1258
   103
 * </pre></blockquote><hr>
jaroslav@1258
   104
 * <p>
jaroslav@1258
   105
 * The following code would then create a thread and start it running:
jaroslav@1258
   106
 * <p><blockquote><pre>
jaroslav@1258
   107
 *     PrimeRun p = new PrimeRun(143);
jaroslav@1258
   108
 *     new Thread(p).start();
jaroslav@1258
   109
 * </pre></blockquote>
jaroslav@1258
   110
 * <p>
jaroslav@1258
   111
 * Every thread has a name for identification purposes. More than
jaroslav@1258
   112
 * one thread may have the same name. If a name is not specified when
jaroslav@1258
   113
 * a thread is created, a new name is generated for it.
jaroslav@1258
   114
 * <p>
jaroslav@1258
   115
 * Unless otherwise noted, passing a {@code null} argument to a constructor
jaroslav@1258
   116
 * or method in this class will cause a {@link NullPointerException} to be
jaroslav@1258
   117
 * thrown.
jaroslav@1258
   118
 *
jaroslav@1258
   119
 * @author  unascribed
jaroslav@1258
   120
 * @see     Runnable
jaroslav@1258
   121
 * @see     Runtime#exit(int)
jaroslav@1258
   122
 * @see     #run()
jaroslav@1258
   123
 * @see     #stop()
jaroslav@1258
   124
 * @since   JDK1.0
jaroslav@1258
   125
 */
jaroslav@1258
   126
public
jaroslav@1258
   127
class Thread implements Runnable {
jaroslav@1258
   128
jaroslav@1258
   129
    /**
jaroslav@1258
   130
     * The minimum priority that a thread can have.
jaroslav@1258
   131
     */
jaroslav@1258
   132
    public final static int MIN_PRIORITY = 1;
jaroslav@1258
   133
jaroslav@1258
   134
   /**
jaroslav@1258
   135
     * The default priority that is assigned to a thread.
jaroslav@1258
   136
     */
jaroslav@1258
   137
    public final static int NORM_PRIORITY = 5;
jaroslav@1258
   138
jaroslav@1258
   139
    /**
jaroslav@1258
   140
     * The maximum priority that a thread can have.
jaroslav@1258
   141
     */
jaroslav@1258
   142
    public final static int MAX_PRIORITY = 10;
jaroslav@1258
   143
jaroslav@1260
   144
    private static final Thread ONE = new Thread("main");
jaroslav@1258
   145
    /**
jaroslav@1258
   146
     * Returns a reference to the currently executing thread object.
jaroslav@1258
   147
     *
jaroslav@1258
   148
     * @return  the currently executing thread.
jaroslav@1258
   149
     */
jaroslav@1260
   150
    public static Thread currentThread() {
jaroslav@1260
   151
        return ONE;
jaroslav@1260
   152
    }
jaroslav@1258
   153
jaroslav@1258
   154
    /**
jaroslav@1258
   155
     * A hint to the scheduler that the current thread is willing to yield
jaroslav@1258
   156
     * its current use of a processor. The scheduler is free to ignore this
jaroslav@1258
   157
     * hint.
jaroslav@1258
   158
     *
jaroslav@1258
   159
     * <p> Yield is a heuristic attempt to improve relative progression
jaroslav@1258
   160
     * between threads that would otherwise over-utilise a CPU. Its use
jaroslav@1258
   161
     * should be combined with detailed profiling and benchmarking to
jaroslav@1258
   162
     * ensure that it actually has the desired effect.
jaroslav@1258
   163
     *
jaroslav@1258
   164
     * <p> It is rarely appropriate to use this method. It may be useful
jaroslav@1258
   165
     * for debugging or testing purposes, where it may help to reproduce
jaroslav@1258
   166
     * bugs due to race conditions. It may also be useful when designing
jaroslav@1258
   167
     * concurrency control constructs such as the ones in the
jaroslav@1258
   168
     * {@link java.util.concurrent.locks} package.
jaroslav@1258
   169
     */
jaroslav@1260
   170
    public static void yield() {
jaroslav@1260
   171
    }
jaroslav@1258
   172
jaroslav@1258
   173
    /**
jaroslav@1258
   174
     * Causes the currently executing thread to sleep (temporarily cease
jaroslav@1258
   175
     * execution) for the specified number of milliseconds, subject to
jaroslav@1258
   176
     * the precision and accuracy of system timers and schedulers. The thread
jaroslav@1258
   177
     * does not lose ownership of any monitors.
jaroslav@1258
   178
     *
jaroslav@1258
   179
     * @param  millis
jaroslav@1258
   180
     *         the length of time to sleep in milliseconds
jaroslav@1258
   181
     *
jaroslav@1258
   182
     * @throws  IllegalArgumentException
jaroslav@1258
   183
     *          if the value of {@code millis} is negative
jaroslav@1258
   184
     *
jaroslav@1258
   185
     * @throws  InterruptedException
jaroslav@1258
   186
     *          if any thread has interrupted the current thread. The
jaroslav@1258
   187
     *          <i>interrupted status</i> of the current thread is
jaroslav@1258
   188
     *          cleared when this exception is thrown.
jaroslav@1258
   189
     */
jaroslav@1258
   190
    public static native void sleep(long millis) throws InterruptedException;
jaroslav@1258
   191
jaroslav@1258
   192
    /**
jaroslav@1258
   193
     * Causes the currently executing thread to sleep (temporarily cease
jaroslav@1258
   194
     * execution) for the specified number of milliseconds plus the specified
jaroslav@1258
   195
     * number of nanoseconds, subject to the precision and accuracy of system
jaroslav@1258
   196
     * timers and schedulers. The thread does not lose ownership of any
jaroslav@1258
   197
     * monitors.
jaroslav@1258
   198
     *
jaroslav@1258
   199
     * @param  millis
jaroslav@1258
   200
     *         the length of time to sleep in milliseconds
jaroslav@1258
   201
     *
jaroslav@1258
   202
     * @param  nanos
jaroslav@1258
   203
     *         {@code 0-999999} additional nanoseconds to sleep
jaroslav@1258
   204
     *
jaroslav@1258
   205
     * @throws  IllegalArgumentException
jaroslav@1258
   206
     *          if the value of {@code millis} is negative, or the value of
jaroslav@1258
   207
     *          {@code nanos} is not in the range {@code 0-999999}
jaroslav@1258
   208
     *
jaroslav@1258
   209
     * @throws  InterruptedException
jaroslav@1258
   210
     *          if any thread has interrupted the current thread. The
jaroslav@1258
   211
     *          <i>interrupted status</i> of the current thread is
jaroslav@1258
   212
     *          cleared when this exception is thrown.
jaroslav@1258
   213
     */
jaroslav@1258
   214
    public static void sleep(long millis, int nanos)
jaroslav@1258
   215
    throws InterruptedException {
jaroslav@1258
   216
        if (millis < 0) {
jaroslav@1258
   217
            throw new IllegalArgumentException("timeout value is negative");
jaroslav@1258
   218
        }
jaroslav@1258
   219
jaroslav@1258
   220
        if (nanos < 0 || nanos > 999999) {
jaroslav@1258
   221
            throw new IllegalArgumentException(
jaroslav@1258
   222
                                "nanosecond timeout value out of range");
jaroslav@1258
   223
        }
jaroslav@1258
   224
jaroslav@1258
   225
        if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
jaroslav@1258
   226
            millis++;
jaroslav@1258
   227
        }
jaroslav@1258
   228
jaroslav@1258
   229
        sleep(millis);
jaroslav@1258
   230
    }
jaroslav@1260
   231
    private Runnable target;
jaroslav@1260
   232
    private String name;
jaroslav@1258
   233
jaroslav@1258
   234
    /**
jaroslav@1258
   235
     * Throws CloneNotSupportedException as a Thread can not be meaningfully
jaroslav@1258
   236
     * cloned. Construct a new Thread instead.
jaroslav@1258
   237
     *
jaroslav@1258
   238
     * @throws  CloneNotSupportedException
jaroslav@1258
   239
     *          always
jaroslav@1258
   240
     */
jaroslav@1258
   241
    @Override
jaroslav@1258
   242
    protected Object clone() throws CloneNotSupportedException {
jaroslav@1258
   243
        throw new CloneNotSupportedException();
jaroslav@1258
   244
    }
jaroslav@1258
   245
jaroslav@1258
   246
    /**
jaroslav@1258
   247
     * Allocates a new {@code Thread} object. This constructor has the same
jaroslav@1258
   248
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
jaroslav@1258
   249
     * {@code (null, null, gname)}, where {@code gname} is a newly generated
jaroslav@1258
   250
     * name. Automatically generated names are of the form
jaroslav@1258
   251
     * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
jaroslav@1258
   252
     */
jaroslav@1258
   253
    public Thread() {
jaroslav@1258
   254
        init(null, null, "Thread-" + nextThreadNum(), 0);
jaroslav@1258
   255
    }
jaroslav@1260
   256
    
jaroslav@1260
   257
    private static int nextThreadNum() {
jaroslav@1260
   258
        return -1;
jaroslav@1260
   259
    }
jaroslav@1258
   260
jaroslav@1258
   261
    /**
jaroslav@1258
   262
     * Allocates a new {@code Thread} object. This constructor has the same
jaroslav@1258
   263
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
jaroslav@1258
   264
     * {@code (null, target, gname)}, where {@code gname} is a newly generated
jaroslav@1258
   265
     * name. Automatically generated names are of the form
jaroslav@1258
   266
     * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
jaroslav@1258
   267
     *
jaroslav@1258
   268
     * @param  target
jaroslav@1258
   269
     *         the object whose {@code run} method is invoked when this thread
jaroslav@1258
   270
     *         is started. If {@code null}, this classes {@code run} method does
jaroslav@1258
   271
     *         nothing.
jaroslav@1258
   272
     */
jaroslav@1258
   273
    public Thread(Runnable target) {
jaroslav@1258
   274
        init(null, target, "Thread-" + nextThreadNum(), 0);
jaroslav@1258
   275
    }
jaroslav@1258
   276
jaroslav@1258
   277
    /**
jaroslav@1258
   278
     * Allocates a new {@code Thread} object. This constructor has the same
jaroslav@1258
   279
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
jaroslav@1258
   280
     * {@code (group, target, gname)} ,where {@code gname} is a newly generated
jaroslav@1258
   281
     * name. Automatically generated names are of the form
jaroslav@1258
   282
     * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
jaroslav@1258
   283
     *
jaroslav@1258
   284
     * @param  group
jaroslav@1258
   285
     *         the thread group. If {@code null} and there is a security
jaroslav@1258
   286
     *         manager, the group is determined by {@linkplain
jaroslav@1258
   287
     *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
jaroslav@1258
   288
     *         If there is not a security manager or {@code
jaroslav@1258
   289
     *         SecurityManager.getThreadGroup()} returns {@code null}, the group
jaroslav@1258
   290
     *         is set to the current thread's thread group.
jaroslav@1258
   291
     *
jaroslav@1258
   292
     * @param  target
jaroslav@1258
   293
     *         the object whose {@code run} method is invoked when this thread
jaroslav@1258
   294
     *         is started. If {@code null}, this thread's run method is invoked.
jaroslav@1258
   295
     *
jaroslav@1258
   296
     * @throws  SecurityException
jaroslav@1258
   297
     *          if the current thread cannot create a thread in the specified
jaroslav@1258
   298
     *          thread group
jaroslav@1258
   299
     */
jaroslav@1260
   300
//    public Thread(ThreadGroup group, Runnable target) {
jaroslav@1260
   301
//        init(group, target, "Thread-" + nextThreadNum(), 0);
jaroslav@1260
   302
//    }
jaroslav@1258
   303
jaroslav@1258
   304
    /**
jaroslav@1258
   305
     * Allocates a new {@code Thread} object. This constructor has the same
jaroslav@1258
   306
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
jaroslav@1258
   307
     * {@code (null, null, name)}.
jaroslav@1258
   308
     *
jaroslav@1258
   309
     * @param   name
jaroslav@1258
   310
     *          the name of the new thread
jaroslav@1258
   311
     */
jaroslav@1258
   312
    public Thread(String name) {
jaroslav@1258
   313
        init(null, null, name, 0);
jaroslav@1258
   314
    }
jaroslav@1260
   315
    
jaroslav@1260
   316
    private void init(Object o1, Runnable trgt, String nm, int i4) {
jaroslav@1260
   317
        this.target = trgt;
jaroslav@1260
   318
        this.name = nm;
jaroslav@1260
   319
    }
jaroslav@1258
   320
jaroslav@1258
   321
    /**
jaroslav@1258
   322
     * Allocates a new {@code Thread} object. This constructor has the same
jaroslav@1258
   323
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
jaroslav@1258
   324
     * {@code (group, null, name)}.
jaroslav@1258
   325
     *
jaroslav@1258
   326
     * @param  group
jaroslav@1258
   327
     *         the thread group. If {@code null} and there is a security
jaroslav@1258
   328
     *         manager, the group is determined by {@linkplain
jaroslav@1258
   329
     *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
jaroslav@1258
   330
     *         If there is not a security manager or {@code
jaroslav@1258
   331
     *         SecurityManager.getThreadGroup()} returns {@code null}, the group
jaroslav@1258
   332
     *         is set to the current thread's thread group.
jaroslav@1258
   333
     *
jaroslav@1258
   334
     * @param  name
jaroslav@1258
   335
     *         the name of the new thread
jaroslav@1258
   336
     *
jaroslav@1258
   337
     * @throws  SecurityException
jaroslav@1258
   338
     *          if the current thread cannot create a thread in the specified
jaroslav@1258
   339
     *          thread group
jaroslav@1258
   340
     */
jaroslav@1260
   341
//    public Thread(ThreadGroup group, String name) {
jaroslav@1260
   342
//        init(group, null, name, 0);
jaroslav@1260
   343
//    }
jaroslav@1258
   344
jaroslav@1258
   345
    /**
jaroslav@1258
   346
     * Allocates a new {@code Thread} object. This constructor has the same
jaroslav@1258
   347
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
jaroslav@1258
   348
     * {@code (null, target, name)}.
jaroslav@1258
   349
     *
jaroslav@1258
   350
     * @param  target
jaroslav@1258
   351
     *         the object whose {@code run} method is invoked when this thread
jaroslav@1258
   352
     *         is started. If {@code null}, this thread's run method is invoked.
jaroslav@1258
   353
     *
jaroslav@1258
   354
     * @param  name
jaroslav@1258
   355
     *         the name of the new thread
jaroslav@1258
   356
     */
jaroslav@1258
   357
    public Thread(Runnable target, String name) {
jaroslav@1258
   358
        init(null, target, name, 0);
jaroslav@1258
   359
    }
jaroslav@1258
   360
jaroslav@1258
   361
    /**
jaroslav@1258
   362
     * Allocates a new {@code Thread} object so that it has {@code target}
jaroslav@1258
   363
     * as its run object, has the specified {@code name} as its name,
jaroslav@1258
   364
     * and belongs to the thread group referred to by {@code group}.
jaroslav@1258
   365
     *
jaroslav@1258
   366
     * <p>If there is a security manager, its
jaroslav@1258
   367
     * {@link SecurityManager#checkAccess(ThreadGroup) checkAccess}
jaroslav@1258
   368
     * method is invoked with the ThreadGroup as its argument.
jaroslav@1258
   369
     *
jaroslav@1258
   370
     * <p>In addition, its {@code checkPermission} method is invoked with
jaroslav@1258
   371
     * the {@code RuntimePermission("enableContextClassLoaderOverride")}
jaroslav@1258
   372
     * permission when invoked directly or indirectly by the constructor
jaroslav@1258
   373
     * of a subclass which overrides the {@code getContextClassLoader}
jaroslav@1258
   374
     * or {@code setContextClassLoader} methods.
jaroslav@1258
   375
     *
jaroslav@1258
   376
     * <p>The priority of the newly created thread is set equal to the
jaroslav@1258
   377
     * priority of the thread creating it, that is, the currently running
jaroslav@1258
   378
     * thread. The method {@linkplain #setPriority setPriority} may be
jaroslav@1258
   379
     * used to change the priority to a new value.
jaroslav@1258
   380
     *
jaroslav@1258
   381
     * <p>The newly created thread is initially marked as being a daemon
jaroslav@1258
   382
     * thread if and only if the thread creating it is currently marked
jaroslav@1258
   383
     * as a daemon thread. The method {@linkplain #setDaemon setDaemon}
jaroslav@1258
   384
     * may be used to change whether or not a thread is a daemon.
jaroslav@1258
   385
     *
jaroslav@1258
   386
     * @param  group
jaroslav@1258
   387
     *         the thread group. If {@code null} and there is a security
jaroslav@1258
   388
     *         manager, the group is determined by {@linkplain
jaroslav@1258
   389
     *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
jaroslav@1258
   390
     *         If there is not a security manager or {@code
jaroslav@1258
   391
     *         SecurityManager.getThreadGroup()} returns {@code null}, the group
jaroslav@1258
   392
     *         is set to the current thread's thread group.
jaroslav@1258
   393
     *
jaroslav@1258
   394
     * @param  target
jaroslav@1258
   395
     *         the object whose {@code run} method is invoked when this thread
jaroslav@1258
   396
     *         is started. If {@code null}, this thread's run method is invoked.
jaroslav@1258
   397
     *
jaroslav@1258
   398
     * @param  name
jaroslav@1258
   399
     *         the name of the new thread
jaroslav@1258
   400
     *
jaroslav@1258
   401
     * @throws  SecurityException
jaroslav@1258
   402
     *          if the current thread cannot create a thread in the specified
jaroslav@1258
   403
     *          thread group or cannot override the context class loader methods.
jaroslav@1258
   404
     */
jaroslav@1260
   405
//    public Thread(ThreadGroup group, Runnable target, String name) {
jaroslav@1260
   406
//        init(group, target, name, 0);
jaroslav@1260
   407
//    }
jaroslav@1258
   408
jaroslav@1258
   409
    /**
jaroslav@1258
   410
     * Allocates a new {@code Thread} object so that it has {@code target}
jaroslav@1258
   411
     * as its run object, has the specified {@code name} as its name,
jaroslav@1258
   412
     * and belongs to the thread group referred to by {@code group}, and has
jaroslav@1258
   413
     * the specified <i>stack size</i>.
jaroslav@1258
   414
     *
jaroslav@1258
   415
     * <p>This constructor is identical to {@link
jaroslav@1258
   416
     * #Thread(ThreadGroup,Runnable,String)} with the exception of the fact
jaroslav@1258
   417
     * that it allows the thread stack size to be specified.  The stack size
jaroslav@1258
   418
     * is the approximate number of bytes of address space that the virtual
jaroslav@1258
   419
     * machine is to allocate for this thread's stack.  <b>The effect of the
jaroslav@1258
   420
     * {@code stackSize} parameter, if any, is highly platform dependent.</b>
jaroslav@1258
   421
     *
jaroslav@1258
   422
     * <p>On some platforms, specifying a higher value for the
jaroslav@1258
   423
     * {@code stackSize} parameter may allow a thread to achieve greater
jaroslav@1258
   424
     * recursion depth before throwing a {@link StackOverflowError}.
jaroslav@1258
   425
     * Similarly, specifying a lower value may allow a greater number of
jaroslav@1258
   426
     * threads to exist concurrently without throwing an {@link
jaroslav@1258
   427
     * OutOfMemoryError} (or other internal error).  The details of
jaroslav@1258
   428
     * the relationship between the value of the <tt>stackSize</tt> parameter
jaroslav@1258
   429
     * and the maximum recursion depth and concurrency level are
jaroslav@1258
   430
     * platform-dependent.  <b>On some platforms, the value of the
jaroslav@1258
   431
     * {@code stackSize} parameter may have no effect whatsoever.</b>
jaroslav@1258
   432
     *
jaroslav@1258
   433
     * <p>The virtual machine is free to treat the {@code stackSize}
jaroslav@1258
   434
     * parameter as a suggestion.  If the specified value is unreasonably low
jaroslav@1258
   435
     * for the platform, the virtual machine may instead use some
jaroslav@1258
   436
     * platform-specific minimum value; if the specified value is unreasonably
jaroslav@1258
   437
     * high, the virtual machine may instead use some platform-specific
jaroslav@1258
   438
     * maximum.  Likewise, the virtual machine is free to round the specified
jaroslav@1258
   439
     * value up or down as it sees fit (or to ignore it completely).
jaroslav@1258
   440
     *
jaroslav@1258
   441
     * <p>Specifying a value of zero for the {@code stackSize} parameter will
jaroslav@1258
   442
     * cause this constructor to behave exactly like the
jaroslav@1258
   443
     * {@code Thread(ThreadGroup, Runnable, String)} constructor.
jaroslav@1258
   444
     *
jaroslav@1258
   445
     * <p><i>Due to the platform-dependent nature of the behavior of this
jaroslav@1258
   446
     * constructor, extreme care should be exercised in its use.
jaroslav@1258
   447
     * The thread stack size necessary to perform a given computation will
jaroslav@1258
   448
     * likely vary from one JRE implementation to another.  In light of this
jaroslav@1258
   449
     * variation, careful tuning of the stack size parameter may be required,
jaroslav@1258
   450
     * and the tuning may need to be repeated for each JRE implementation on
jaroslav@1258
   451
     * which an application is to run.</i>
jaroslav@1258
   452
     *
jaroslav@1258
   453
     * <p>Implementation note: Java platform implementers are encouraged to
jaroslav@1258
   454
     * document their implementation's behavior with respect to the
jaroslav@1258
   455
     * {@code stackSize} parameter.
jaroslav@1258
   456
     *
jaroslav@1258
   457
     *
jaroslav@1258
   458
     * @param  group
jaroslav@1258
   459
     *         the thread group. If {@code null} and there is a security
jaroslav@1258
   460
     *         manager, the group is determined by {@linkplain
jaroslav@1258
   461
     *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
jaroslav@1258
   462
     *         If there is not a security manager or {@code
jaroslav@1258
   463
     *         SecurityManager.getThreadGroup()} returns {@code null}, the group
jaroslav@1258
   464
     *         is set to the current thread's thread group.
jaroslav@1258
   465
     *
jaroslav@1258
   466
     * @param  target
jaroslav@1258
   467
     *         the object whose {@code run} method is invoked when this thread
jaroslav@1258
   468
     *         is started. If {@code null}, this thread's run method is invoked.
jaroslav@1258
   469
     *
jaroslav@1258
   470
     * @param  name
jaroslav@1258
   471
     *         the name of the new thread
jaroslav@1258
   472
     *
jaroslav@1258
   473
     * @param  stackSize
jaroslav@1258
   474
     *         the desired stack size for the new thread, or zero to indicate
jaroslav@1258
   475
     *         that this parameter is to be ignored.
jaroslav@1258
   476
     *
jaroslav@1258
   477
     * @throws  SecurityException
jaroslav@1258
   478
     *          if the current thread cannot create a thread in the specified
jaroslav@1258
   479
     *          thread group
jaroslav@1258
   480
     *
jaroslav@1258
   481
     * @since 1.4
jaroslav@1258
   482
     */
jaroslav@1260
   483
//    public Thread(ThreadGroup group, Runnable target, String name,
jaroslav@1260
   484
//                  long stackSize) {
jaroslav@1260
   485
//        init(group, target, name, stackSize);
jaroslav@1260
   486
//    }
jaroslav@1258
   487
jaroslav@1258
   488
    /**
jaroslav@1258
   489
     * Causes this thread to begin execution; the Java Virtual Machine
jaroslav@1258
   490
     * calls the <code>run</code> method of this thread.
jaroslav@1258
   491
     * <p>
jaroslav@1258
   492
     * The result is that two threads are running concurrently: the
jaroslav@1258
   493
     * current thread (which returns from the call to the
jaroslav@1258
   494
     * <code>start</code> method) and the other thread (which executes its
jaroslav@1258
   495
     * <code>run</code> method).
jaroslav@1258
   496
     * <p>
jaroslav@1258
   497
     * It is never legal to start a thread more than once.
jaroslav@1258
   498
     * In particular, a thread may not be restarted once it has completed
jaroslav@1258
   499
     * execution.
jaroslav@1258
   500
     *
jaroslav@1258
   501
     * @exception  IllegalThreadStateException  if the thread was already
jaroslav@1258
   502
     *               started.
jaroslav@1258
   503
     * @see        #run()
jaroslav@1258
   504
     * @see        #stop()
jaroslav@1258
   505
     */
jaroslav@1260
   506
    public void start() {
jaroslav@1260
   507
        throw new SecurityException();
jaroslav@1258
   508
    }
jaroslav@1258
   509
jaroslav@1258
   510
    /**
jaroslav@1258
   511
     * If this thread was constructed using a separate
jaroslav@1258
   512
     * <code>Runnable</code> run object, then that
jaroslav@1258
   513
     * <code>Runnable</code> object's <code>run</code> method is called;
jaroslav@1258
   514
     * otherwise, this method does nothing and returns.
jaroslav@1258
   515
     * <p>
jaroslav@1258
   516
     * Subclasses of <code>Thread</code> should override this method.
jaroslav@1258
   517
     *
jaroslav@1258
   518
     * @see     #start()
jaroslav@1258
   519
     * @see     #stop()
jaroslav@1258
   520
     * @see     #Thread(ThreadGroup, Runnable, String)
jaroslav@1258
   521
     */
jaroslav@1258
   522
    @Override
jaroslav@1258
   523
    public void run() {
jaroslav@1258
   524
        if (target != null) {
jaroslav@1258
   525
            target.run();
jaroslav@1258
   526
        }
jaroslav@1258
   527
    }
jaroslav@1258
   528
jaroslav@1258
   529
    /**
jaroslav@1258
   530
     * Forces the thread to stop executing.
jaroslav@1258
   531
     * <p>
jaroslav@1258
   532
     * If there is a security manager installed, its <code>checkAccess</code>
jaroslav@1258
   533
     * method is called with <code>this</code>
jaroslav@1258
   534
     * as its argument. This may result in a
jaroslav@1258
   535
     * <code>SecurityException</code> being raised (in the current thread).
jaroslav@1258
   536
     * <p>
jaroslav@1258
   537
     * If this thread is different from the current thread (that is, the current
jaroslav@1258
   538
     * thread is trying to stop a thread other than itself), the
jaroslav@1258
   539
     * security manager's <code>checkPermission</code> method (with a
jaroslav@1258
   540
     * <code>RuntimePermission("stopThread")</code> argument) is called in
jaroslav@1258
   541
     * addition.
jaroslav@1258
   542
     * Again, this may result in throwing a
jaroslav@1258
   543
     * <code>SecurityException</code> (in the current thread).
jaroslav@1258
   544
     * <p>
jaroslav@1258
   545
     * The thread represented by this thread is forced to stop whatever
jaroslav@1258
   546
     * it is doing abnormally and to throw a newly created
jaroslav@1258
   547
     * <code>ThreadDeath</code> object as an exception.
jaroslav@1258
   548
     * <p>
jaroslav@1258
   549
     * It is permitted to stop a thread that has not yet been started.
jaroslav@1258
   550
     * If the thread is eventually started, it immediately terminates.
jaroslav@1258
   551
     * <p>
jaroslav@1258
   552
     * An application should not normally try to catch
jaroslav@1258
   553
     * <code>ThreadDeath</code> unless it must do some extraordinary
jaroslav@1258
   554
     * cleanup operation (note that the throwing of
jaroslav@1258
   555
     * <code>ThreadDeath</code> causes <code>finally</code> clauses of
jaroslav@1258
   556
     * <code>try</code> statements to be executed before the thread
jaroslav@1258
   557
     * officially dies).  If a <code>catch</code> clause catches a
jaroslav@1258
   558
     * <code>ThreadDeath</code> object, it is important to rethrow the
jaroslav@1258
   559
     * object so that the thread actually dies.
jaroslav@1258
   560
     * <p>
jaroslav@1258
   561
     * The top-level error handler that reacts to otherwise uncaught
jaroslav@1258
   562
     * exceptions does not print out a message or otherwise notify the
jaroslav@1258
   563
     * application if the uncaught exception is an instance of
jaroslav@1258
   564
     * <code>ThreadDeath</code>.
jaroslav@1258
   565
     *
jaroslav@1258
   566
     * @exception  SecurityException  if the current thread cannot
jaroslav@1258
   567
     *               modify this thread.
jaroslav@1258
   568
     * @see        #interrupt()
jaroslav@1258
   569
     * @see        #checkAccess()
jaroslav@1258
   570
     * @see        #run()
jaroslav@1258
   571
     * @see        #start()
jaroslav@1258
   572
     * @see        ThreadDeath
jaroslav@1258
   573
     * @see        ThreadGroup#uncaughtException(Thread,Throwable)
jaroslav@1258
   574
     * @see        SecurityManager#checkAccess(Thread)
jaroslav@1258
   575
     * @see        SecurityManager#checkPermission
jaroslav@1258
   576
     * @deprecated This method is inherently unsafe.  Stopping a thread with
jaroslav@1258
   577
     *       Thread.stop causes it to unlock all of the monitors that it
jaroslav@1258
   578
     *       has locked (as a natural consequence of the unchecked
jaroslav@1258
   579
     *       <code>ThreadDeath</code> exception propagating up the stack).  If
jaroslav@1258
   580
     *       any of the objects previously protected by these monitors were in
jaroslav@1258
   581
     *       an inconsistent state, the damaged objects become visible to
jaroslav@1258
   582
     *       other threads, potentially resulting in arbitrary behavior.  Many
jaroslav@1258
   583
     *       uses of <code>stop</code> should be replaced by code that simply
jaroslav@1258
   584
     *       modifies some variable to indicate that the target thread should
jaroslav@1258
   585
     *       stop running.  The target thread should check this variable
jaroslav@1258
   586
     *       regularly, and return from its run method in an orderly fashion
jaroslav@1258
   587
     *       if the variable indicates that it is to stop running.  If the
jaroslav@1258
   588
     *       target thread waits for long periods (on a condition variable,
jaroslav@1258
   589
     *       for example), the <code>interrupt</code> method should be used to
jaroslav@1258
   590
     *       interrupt the wait.
jaroslav@1258
   591
     *       For more information, see
jaroslav@1258
   592
     *       <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
jaroslav@1258
   593
     *       are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
jaroslav@1258
   594
     */
jaroslav@1258
   595
    @Deprecated
jaroslav@1258
   596
    public final void stop() {
jaroslav@1260
   597
        stop(null);
jaroslav@1258
   598
    }
jaroslav@1258
   599
jaroslav@1258
   600
    /**
jaroslav@1258
   601
     * Forces the thread to stop executing.
jaroslav@1258
   602
     * <p>
jaroslav@1258
   603
     * If there is a security manager installed, the <code>checkAccess</code>
jaroslav@1258
   604
     * method of this thread is called, which may result in a
jaroslav@1258
   605
     * <code>SecurityException</code> being raised (in the current thread).
jaroslav@1258
   606
     * <p>
jaroslav@1258
   607
     * If this thread is different from the current thread (that is, the current
jaroslav@1258
   608
     * thread is trying to stop a thread other than itself) or
jaroslav@1258
   609
     * <code>obj</code> is not an instance of <code>ThreadDeath</code>, the
jaroslav@1258
   610
     * security manager's <code>checkPermission</code> method (with the
jaroslav@1258
   611
     * <code>RuntimePermission("stopThread")</code> argument) is called in
jaroslav@1258
   612
     * addition.
jaroslav@1258
   613
     * Again, this may result in throwing a
jaroslav@1258
   614
     * <code>SecurityException</code> (in the current thread).
jaroslav@1258
   615
     * <p>
jaroslav@1258
   616
     * If the argument <code>obj</code> is null, a
jaroslav@1258
   617
     * <code>NullPointerException</code> is thrown (in the current thread).
jaroslav@1258
   618
     * <p>
jaroslav@1258
   619
     * The thread represented by this thread is forced to stop
jaroslav@1258
   620
     * whatever it is doing abnormally and to throw the
jaroslav@1258
   621
     * <code>Throwable</code> object <code>obj</code> as an exception. This
jaroslav@1258
   622
     * is an unusual action to take; normally, the <code>stop</code> method
jaroslav@1258
   623
     * that takes no arguments should be used.
jaroslav@1258
   624
     * <p>
jaroslav@1258
   625
     * It is permitted to stop a thread that has not yet been started.
jaroslav@1258
   626
     * If the thread is eventually started, it immediately terminates.
jaroslav@1258
   627
     *
jaroslav@1258
   628
     * @param      obj   the Throwable object to be thrown.
jaroslav@1258
   629
     * @exception  SecurityException  if the current thread cannot modify
jaroslav@1258
   630
     *               this thread.
jaroslav@1258
   631
     * @throws     NullPointerException if obj is <tt>null</tt>.
jaroslav@1258
   632
     * @see        #interrupt()
jaroslav@1258
   633
     * @see        #checkAccess()
jaroslav@1258
   634
     * @see        #run()
jaroslav@1258
   635
     * @see        #start()
jaroslav@1258
   636
     * @see        #stop()
jaroslav@1258
   637
     * @see        SecurityManager#checkAccess(Thread)
jaroslav@1258
   638
     * @see        SecurityManager#checkPermission
jaroslav@1258
   639
     * @deprecated This method is inherently unsafe.  See {@link #stop()}
jaroslav@1258
   640
     *        for details.  An additional danger of this
jaroslav@1258
   641
     *        method is that it may be used to generate exceptions that the
jaroslav@1258
   642
     *        target thread is unprepared to handle (including checked
jaroslav@1258
   643
     *        exceptions that the thread could not possibly throw, were it
jaroslav@1258
   644
     *        not for this method).
jaroslav@1258
   645
     *        For more information, see
jaroslav@1258
   646
     *        <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
jaroslav@1258
   647
     *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
jaroslav@1258
   648
     */
jaroslav@1258
   649
    @Deprecated
jaroslav@1258
   650
    public final synchronized void stop(Throwable obj) {
jaroslav@1260
   651
        throw new SecurityException();
jaroslav@1258
   652
    }
jaroslav@1258
   653
jaroslav@1258
   654
    /**
jaroslav@1258
   655
     * Interrupts this thread.
jaroslav@1258
   656
     *
jaroslav@1258
   657
     * <p> Unless the current thread is interrupting itself, which is
jaroslav@1258
   658
     * always permitted, the {@link #checkAccess() checkAccess} method
jaroslav@1258
   659
     * of this thread is invoked, which may cause a {@link
jaroslav@1258
   660
     * SecurityException} to be thrown.
jaroslav@1258
   661
     *
jaroslav@1258
   662
     * <p> If this thread is blocked in an invocation of the {@link
jaroslav@1258
   663
     * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
jaroslav@1258
   664
     * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
jaroslav@1258
   665
     * class, or of the {@link #join()}, {@link #join(long)}, {@link
jaroslav@1258
   666
     * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
jaroslav@1258
   667
     * methods of this class, then its interrupt status will be cleared and it
jaroslav@1258
   668
     * will receive an {@link InterruptedException}.
jaroslav@1258
   669
     *
jaroslav@1258
   670
     * <p> If this thread is blocked in an I/O operation upon an {@link
jaroslav@1258
   671
     * java.nio.channels.InterruptibleChannel </code>interruptible
jaroslav@1258
   672
     * channel<code>} then the channel will be closed, the thread's interrupt
jaroslav@1258
   673
     * status will be set, and the thread will receive a {@link
jaroslav@1258
   674
     * java.nio.channels.ClosedByInterruptException}.
jaroslav@1258
   675
     *
jaroslav@1258
   676
     * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
jaroslav@1258
   677
     * then the thread's interrupt status will be set and it will return
jaroslav@1258
   678
     * immediately from the selection operation, possibly with a non-zero
jaroslav@1258
   679
     * value, just as if the selector's {@link
jaroslav@1258
   680
     * java.nio.channels.Selector#wakeup wakeup} method were invoked.
jaroslav@1258
   681
     *
jaroslav@1258
   682
     * <p> If none of the previous conditions hold then this thread's interrupt
jaroslav@1258
   683
     * status will be set. </p>
jaroslav@1258
   684
     *
jaroslav@1258
   685
     * <p> Interrupting a thread that is not alive need not have any effect.
jaroslav@1258
   686
     *
jaroslav@1258
   687
     * @throws  SecurityException
jaroslav@1258
   688
     *          if the current thread cannot modify this thread
jaroslav@1258
   689
     *
jaroslav@1258
   690
     * @revised 6.0
jaroslav@1258
   691
     * @spec JSR-51
jaroslav@1258
   692
     */
jaroslav@1258
   693
    public void interrupt() {
jaroslav@1260
   694
        throw new SecurityException();
jaroslav@1258
   695
    }
jaroslav@1258
   696
jaroslav@1258
   697
    /**
jaroslav@1258
   698
     * Tests whether the current thread has been interrupted.  The
jaroslav@1258
   699
     * <i>interrupted status</i> of the thread is cleared by this method.  In
jaroslav@1258
   700
     * other words, if this method were to be called twice in succession, the
jaroslav@1258
   701
     * second call would return false (unless the current thread were
jaroslav@1258
   702
     * interrupted again, after the first call had cleared its interrupted
jaroslav@1258
   703
     * status and before the second call had examined it).
jaroslav@1258
   704
     *
jaroslav@1258
   705
     * <p>A thread interruption ignored because a thread was not alive
jaroslav@1258
   706
     * at the time of the interrupt will be reflected by this method
jaroslav@1258
   707
     * returning false.
jaroslav@1258
   708
     *
jaroslav@1258
   709
     * @return  <code>true</code> if the current thread has been interrupted;
jaroslav@1258
   710
     *          <code>false</code> otherwise.
jaroslav@1258
   711
     * @see #isInterrupted()
jaroslav@1258
   712
     * @revised 6.0
jaroslav@1258
   713
     */
jaroslav@1258
   714
    public static boolean interrupted() {
jaroslav@1260
   715
        return currentThread().isInterrupted();
jaroslav@1258
   716
    }
jaroslav@1258
   717
jaroslav@1258
   718
    /**
jaroslav@1258
   719
     * Tests whether this thread has been interrupted.  The <i>interrupted
jaroslav@1258
   720
     * status</i> of the thread is unaffected by this method.
jaroslav@1258
   721
     *
jaroslav@1258
   722
     * <p>A thread interruption ignored because a thread was not alive
jaroslav@1258
   723
     * at the time of the interrupt will be reflected by this method
jaroslav@1258
   724
     * returning false.
jaroslav@1258
   725
     *
jaroslav@1258
   726
     * @return  <code>true</code> if this thread has been interrupted;
jaroslav@1258
   727
     *          <code>false</code> otherwise.
jaroslav@1258
   728
     * @see     #interrupted()
jaroslav@1258
   729
     * @revised 6.0
jaroslav@1258
   730
     */
jaroslav@1258
   731
    public boolean isInterrupted() {
jaroslav@1260
   732
        return false;
jaroslav@1258
   733
    }
jaroslav@1258
   734
jaroslav@1258
   735
    /**
jaroslav@1258
   736
     * Throws {@link NoSuchMethodError}.
jaroslav@1258
   737
     *
jaroslav@1258
   738
     * @deprecated This method was originally designed to destroy this
jaroslav@1258
   739
     *     thread without any cleanup. Any monitors it held would have
jaroslav@1258
   740
     *     remained locked. However, the method was never implemented.
jaroslav@1258
   741
     *     If if were to be implemented, it would be deadlock-prone in
jaroslav@1258
   742
     *     much the manner of {@link #suspend}. If the target thread held
jaroslav@1258
   743
     *     a lock protecting a critical system resource when it was
jaroslav@1258
   744
     *     destroyed, no thread could ever access this resource again.
jaroslav@1258
   745
     *     If another thread ever attempted to lock this resource, deadlock
jaroslav@1258
   746
     *     would result. Such deadlocks typically manifest themselves as
jaroslav@1258
   747
     *     "frozen" processes. For more information, see
jaroslav@1258
   748
     *     <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">
jaroslav@1258
   749
     *     Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
jaroslav@1258
   750
     * @throws NoSuchMethodError always
jaroslav@1258
   751
     */
jaroslav@1258
   752
    @Deprecated
jaroslav@1258
   753
    public void destroy() {
jaroslav@1260
   754
        throw new SecurityException();
jaroslav@1258
   755
    }
jaroslav@1258
   756
jaroslav@1258
   757
    /**
jaroslav@1258
   758
     * Tests if this thread is alive. A thread is alive if it has
jaroslav@1258
   759
     * been started and has not yet died.
jaroslav@1258
   760
     *
jaroslav@1258
   761
     * @return  <code>true</code> if this thread is alive;
jaroslav@1258
   762
     *          <code>false</code> otherwise.
jaroslav@1258
   763
     */
jaroslav@1260
   764
    public final boolean isAlive() {
jaroslav@1260
   765
        return true;
jaroslav@1260
   766
    }
jaroslav@1258
   767
jaroslav@1258
   768
    /**
jaroslav@1258
   769
     * Suspends this thread.
jaroslav@1258
   770
     * <p>
jaroslav@1258
   771
     * First, the <code>checkAccess</code> method of this thread is called
jaroslav@1258
   772
     * with no arguments. This may result in throwing a
jaroslav@1258
   773
     * <code>SecurityException </code>(in the current thread).
jaroslav@1258
   774
     * <p>
jaroslav@1258
   775
     * If the thread is alive, it is suspended and makes no further
jaroslav@1258
   776
     * progress unless and until it is resumed.
jaroslav@1258
   777
     *
jaroslav@1258
   778
     * @exception  SecurityException  if the current thread cannot modify
jaroslav@1258
   779
     *               this thread.
jaroslav@1258
   780
     * @see #checkAccess
jaroslav@1258
   781
     * @deprecated   This method has been deprecated, as it is
jaroslav@1258
   782
     *   inherently deadlock-prone.  If the target thread holds a lock on the
jaroslav@1258
   783
     *   monitor protecting a critical system resource when it is suspended, no
jaroslav@1258
   784
     *   thread can access this resource until the target thread is resumed. If
jaroslav@1258
   785
     *   the thread that would resume the target thread attempts to lock this
jaroslav@1258
   786
     *   monitor prior to calling <code>resume</code>, deadlock results.  Such
jaroslav@1258
   787
     *   deadlocks typically manifest themselves as "frozen" processes.
jaroslav@1258
   788
     *   For more information, see
jaroslav@1258
   789
     *   <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
jaroslav@1258
   790
     *   are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
jaroslav@1258
   791
     */
jaroslav@1258
   792
    @Deprecated
jaroslav@1258
   793
    public final void suspend() {
jaroslav@1258
   794
        checkAccess();
jaroslav@1258
   795
    }
jaroslav@1258
   796
jaroslav@1258
   797
    /**
jaroslav@1258
   798
     * Resumes a suspended thread.
jaroslav@1258
   799
     * <p>
jaroslav@1258
   800
     * First, the <code>checkAccess</code> method of this thread is called
jaroslav@1258
   801
     * with no arguments. This may result in throwing a
jaroslav@1258
   802
     * <code>SecurityException</code> (in the current thread).
jaroslav@1258
   803
     * <p>
jaroslav@1258
   804
     * If the thread is alive but suspended, it is resumed and is
jaroslav@1258
   805
     * permitted to make progress in its execution.
jaroslav@1258
   806
     *
jaroslav@1258
   807
     * @exception  SecurityException  if the current thread cannot modify this
jaroslav@1258
   808
     *               thread.
jaroslav@1258
   809
     * @see        #checkAccess
jaroslav@1258
   810
     * @see        #suspend()
jaroslav@1258
   811
     * @deprecated This method exists solely for use with {@link #suspend},
jaroslav@1258
   812
     *     which has been deprecated because it is deadlock-prone.
jaroslav@1258
   813
     *     For more information, see
jaroslav@1258
   814
     *     <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
jaroslav@1258
   815
     *     are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
jaroslav@1258
   816
     */
jaroslav@1258
   817
    @Deprecated
jaroslav@1258
   818
    public final void resume() {
jaroslav@1258
   819
        checkAccess();
jaroslav@1258
   820
    }
jaroslav@1258
   821
jaroslav@1258
   822
    /**
jaroslav@1258
   823
     * Changes the priority of this thread.
jaroslav@1258
   824
     * <p>
jaroslav@1258
   825
     * First the <code>checkAccess</code> method of this thread is called
jaroslav@1258
   826
     * with no arguments. This may result in throwing a
jaroslav@1258
   827
     * <code>SecurityException</code>.
jaroslav@1258
   828
     * <p>
jaroslav@1258
   829
     * Otherwise, the priority of this thread is set to the smaller of
jaroslav@1258
   830
     * the specified <code>newPriority</code> and the maximum permitted
jaroslav@1258
   831
     * priority of the thread's thread group.
jaroslav@1258
   832
     *
jaroslav@1258
   833
     * @param newPriority priority to set this thread to
jaroslav@1258
   834
     * @exception  IllegalArgumentException  If the priority is not in the
jaroslav@1258
   835
     *               range <code>MIN_PRIORITY</code> to
jaroslav@1258
   836
     *               <code>MAX_PRIORITY</code>.
jaroslav@1258
   837
     * @exception  SecurityException  if the current thread cannot modify
jaroslav@1258
   838
     *               this thread.
jaroslav@1258
   839
     * @see        #getPriority
jaroslav@1258
   840
     * @see        #checkAccess()
jaroslav@1258
   841
     * @see        #getThreadGroup()
jaroslav@1258
   842
     * @see        #MAX_PRIORITY
jaroslav@1258
   843
     * @see        #MIN_PRIORITY
jaroslav@1258
   844
     * @see        ThreadGroup#getMaxPriority()
jaroslav@1258
   845
     */
jaroslav@1258
   846
    public final void setPriority(int newPriority) {
jaroslav@1260
   847
        throw new SecurityException();
jaroslav@1258
   848
    }
jaroslav@1258
   849
jaroslav@1258
   850
    /**
jaroslav@1258
   851
     * Returns this thread's priority.
jaroslav@1258
   852
     *
jaroslav@1258
   853
     * @return  this thread's priority.
jaroslav@1258
   854
     * @see     #setPriority
jaroslav@1258
   855
     */
jaroslav@1258
   856
    public final int getPriority() {
jaroslav@1260
   857
        return Thread.NORM_PRIORITY;
jaroslav@1258
   858
    }
jaroslav@1258
   859
jaroslav@1258
   860
    /**
jaroslav@1258
   861
     * Changes the name of this thread to be equal to the argument
jaroslav@1258
   862
     * <code>name</code>.
jaroslav@1258
   863
     * <p>
jaroslav@1258
   864
     * First the <code>checkAccess</code> method of this thread is called
jaroslav@1258
   865
     * with no arguments. This may result in throwing a
jaroslav@1258
   866
     * <code>SecurityException</code>.
jaroslav@1258
   867
     *
jaroslav@1258
   868
     * @param      name   the new name for this thread.
jaroslav@1258
   869
     * @exception  SecurityException  if the current thread cannot modify this
jaroslav@1258
   870
     *               thread.
jaroslav@1258
   871
     * @see        #getName
jaroslav@1258
   872
     * @see        #checkAccess()
jaroslav@1258
   873
     */
jaroslav@1258
   874
    public final void setName(String name) {
jaroslav@1260
   875
        throw new SecurityException();
jaroslav@1258
   876
    }
jaroslav@1258
   877
jaroslav@1258
   878
    /**
jaroslav@1258
   879
     * Returns this thread's name.
jaroslav@1258
   880
     *
jaroslav@1258
   881
     * @return  this thread's name.
jaroslav@1258
   882
     * @see     #setName(String)
jaroslav@1258
   883
     */
jaroslav@1258
   884
    public final String getName() {
jaroslav@1258
   885
        return String.valueOf(name);
jaroslav@1258
   886
    }
jaroslav@1258
   887
jaroslav@1258
   888
    /**
jaroslav@1258
   889
     * Returns the thread group to which this thread belongs.
jaroslav@1258
   890
     * This method returns null if this thread has died
jaroslav@1258
   891
     * (been stopped).
jaroslav@1258
   892
     *
jaroslav@1258
   893
     * @return  this thread's thread group.
jaroslav@1258
   894
     */
jaroslav@1260
   895
//    public final ThreadGroup getThreadGroup() {
jaroslav@1260
   896
//        return group;
jaroslav@1260
   897
//    }
jaroslav@1258
   898
jaroslav@1258
   899
    /**
jaroslav@1258
   900
     * Returns an estimate of the number of active threads in the current
jaroslav@1258
   901
     * thread's {@linkplain java.lang.ThreadGroup thread group} and its
jaroslav@1258
   902
     * subgroups. Recursively iterates over all subgroups in the current
jaroslav@1258
   903
     * thread's thread group.
jaroslav@1258
   904
     *
jaroslav@1258
   905
     * <p> The value returned is only an estimate because the number of
jaroslav@1258
   906
     * threads may change dynamically while this method traverses internal
jaroslav@1258
   907
     * data structures, and might be affected by the presence of certain
jaroslav@1258
   908
     * system threads. This method is intended primarily for debugging
jaroslav@1258
   909
     * and monitoring purposes.
jaroslav@1258
   910
     *
jaroslav@1258
   911
     * @return  an estimate of the number of active threads in the current
jaroslav@1258
   912
     *          thread's thread group and in any other thread group that
jaroslav@1258
   913
     *          has the current thread's thread group as an ancestor
jaroslav@1258
   914
     */
jaroslav@1258
   915
    public static int activeCount() {
jaroslav@1260
   916
        return 1;
jaroslav@1258
   917
    }
jaroslav@1258
   918
jaroslav@1258
   919
    /**
jaroslav@1258
   920
     * Copies into the specified array every active thread in the current
jaroslav@1258
   921
     * thread's thread group and its subgroups. This method simply
jaroslav@1258
   922
     * invokes the {@link java.lang.ThreadGroup#enumerate(Thread[])}
jaroslav@1258
   923
     * method of the current thread's thread group.
jaroslav@1258
   924
     *
jaroslav@1258
   925
     * <p> An application might use the {@linkplain #activeCount activeCount}
jaroslav@1258
   926
     * method to get an estimate of how big the array should be, however
jaroslav@1258
   927
     * <i>if the array is too short to hold all the threads, the extra threads
jaroslav@1258
   928
     * are silently ignored.</i>  If it is critical to obtain every active
jaroslav@1258
   929
     * thread in the current thread's thread group and its subgroups, the
jaroslav@1258
   930
     * invoker should verify that the returned int value is strictly less
jaroslav@1258
   931
     * than the length of {@code tarray}.
jaroslav@1258
   932
     *
jaroslav@1258
   933
     * <p> Due to the inherent race condition in this method, it is recommended
jaroslav@1258
   934
     * that the method only be used for debugging and monitoring purposes.
jaroslav@1258
   935
     *
jaroslav@1258
   936
     * @param  tarray
jaroslav@1258
   937
     *         an array into which to put the list of threads
jaroslav@1258
   938
     *
jaroslav@1258
   939
     * @return  the number of threads put into the array
jaroslav@1258
   940
     *
jaroslav@1258
   941
     * @throws  SecurityException
jaroslav@1258
   942
     *          if {@link java.lang.ThreadGroup#checkAccess} determines that
jaroslav@1258
   943
     *          the current thread cannot access its thread group
jaroslav@1258
   944
     */
jaroslav@1258
   945
    public static int enumerate(Thread tarray[]) {
jaroslav@1260
   946
        throw new SecurityException();
jaroslav@1258
   947
    }
jaroslav@1258
   948
jaroslav@1258
   949
    /**
jaroslav@1258
   950
     * Counts the number of stack frames in this thread. The thread must
jaroslav@1258
   951
     * be suspended.
jaroslav@1258
   952
     *
jaroslav@1258
   953
     * @return     the number of stack frames in this thread.
jaroslav@1258
   954
     * @exception  IllegalThreadStateException  if this thread is not
jaroslav@1258
   955
     *             suspended.
jaroslav@1258
   956
     * @deprecated The definition of this call depends on {@link #suspend},
jaroslav@1258
   957
     *             which is deprecated.  Further, the results of this call
jaroslav@1258
   958
     *             were never well-defined.
jaroslav@1258
   959
     */
jaroslav@1258
   960
    @Deprecated
jaroslav@1258
   961
    public native int countStackFrames();
jaroslav@1258
   962
jaroslav@1258
   963
    /**
jaroslav@1258
   964
     * Waits at most {@code millis} milliseconds for this thread to
jaroslav@1258
   965
     * die. A timeout of {@code 0} means to wait forever.
jaroslav@1258
   966
     *
jaroslav@1258
   967
     * <p> This implementation uses a loop of {@code this.wait} calls
jaroslav@1258
   968
     * conditioned on {@code this.isAlive}. As a thread terminates the
jaroslav@1258
   969
     * {@code this.notifyAll} method is invoked. It is recommended that
jaroslav@1258
   970
     * applications not use {@code wait}, {@code notify}, or
jaroslav@1258
   971
     * {@code notifyAll} on {@code Thread} instances.
jaroslav@1258
   972
     *
jaroslav@1258
   973
     * @param  millis
jaroslav@1258
   974
     *         the time to wait in milliseconds
jaroslav@1258
   975
     *
jaroslav@1258
   976
     * @throws  IllegalArgumentException
jaroslav@1258
   977
     *          if the value of {@code millis} is negative
jaroslav@1258
   978
     *
jaroslav@1258
   979
     * @throws  InterruptedException
jaroslav@1258
   980
     *          if any thread has interrupted the current thread. The
jaroslav@1258
   981
     *          <i>interrupted status</i> of the current thread is
jaroslav@1258
   982
     *          cleared when this exception is thrown.
jaroslav@1258
   983
     */
jaroslav@1258
   984
    public final synchronized void join(long millis)
jaroslav@1258
   985
    throws InterruptedException {
jaroslav@1258
   986
        long base = System.currentTimeMillis();
jaroslav@1258
   987
        long now = 0;
jaroslav@1258
   988
jaroslav@1258
   989
        if (millis < 0) {
jaroslav@1258
   990
            throw new IllegalArgumentException("timeout value is negative");
jaroslav@1258
   991
        }
jaroslav@1258
   992
jaroslav@1258
   993
        if (millis == 0) {
jaroslav@1258
   994
            while (isAlive()) {
jaroslav@1258
   995
                wait(0);
jaroslav@1258
   996
            }
jaroslav@1258
   997
        } else {
jaroslav@1258
   998
            while (isAlive()) {
jaroslav@1258
   999
                long delay = millis - now;
jaroslav@1258
  1000
                if (delay <= 0) {
jaroslav@1258
  1001
                    break;
jaroslav@1258
  1002
                }
jaroslav@1258
  1003
                wait(delay);
jaroslav@1258
  1004
                now = System.currentTimeMillis() - base;
jaroslav@1258
  1005
            }
jaroslav@1258
  1006
        }
jaroslav@1258
  1007
    }
jaroslav@1258
  1008
jaroslav@1258
  1009
    /**
jaroslav@1258
  1010
     * Waits at most {@code millis} milliseconds plus
jaroslav@1258
  1011
     * {@code nanos} nanoseconds for this thread to die.
jaroslav@1258
  1012
     *
jaroslav@1258
  1013
     * <p> This implementation uses a loop of {@code this.wait} calls
jaroslav@1258
  1014
     * conditioned on {@code this.isAlive}. As a thread terminates the
jaroslav@1258
  1015
     * {@code this.notifyAll} method is invoked. It is recommended that
jaroslav@1258
  1016
     * applications not use {@code wait}, {@code notify}, or
jaroslav@1258
  1017
     * {@code notifyAll} on {@code Thread} instances.
jaroslav@1258
  1018
     *
jaroslav@1258
  1019
     * @param  millis
jaroslav@1258
  1020
     *         the time to wait in milliseconds
jaroslav@1258
  1021
     *
jaroslav@1258
  1022
     * @param  nanos
jaroslav@1258
  1023
     *         {@code 0-999999} additional nanoseconds to wait
jaroslav@1258
  1024
     *
jaroslav@1258
  1025
     * @throws  IllegalArgumentException
jaroslav@1258
  1026
     *          if the value of {@code millis} is negative, or the value
jaroslav@1258
  1027
     *          of {@code nanos} is not in the range {@code 0-999999}
jaroslav@1258
  1028
     *
jaroslav@1258
  1029
     * @throws  InterruptedException
jaroslav@1258
  1030
     *          if any thread has interrupted the current thread. The
jaroslav@1258
  1031
     *          <i>interrupted status</i> of the current thread is
jaroslav@1258
  1032
     *          cleared when this exception is thrown.
jaroslav@1258
  1033
     */
jaroslav@1258
  1034
    public final synchronized void join(long millis, int nanos)
jaroslav@1258
  1035
    throws InterruptedException {
jaroslav@1258
  1036
jaroslav@1258
  1037
        if (millis < 0) {
jaroslav@1258
  1038
            throw new IllegalArgumentException("timeout value is negative");
jaroslav@1258
  1039
        }
jaroslav@1258
  1040
jaroslav@1258
  1041
        if (nanos < 0 || nanos > 999999) {
jaroslav@1258
  1042
            throw new IllegalArgumentException(
jaroslav@1258
  1043
                                "nanosecond timeout value out of range");
jaroslav@1258
  1044
        }
jaroslav@1258
  1045
jaroslav@1258
  1046
        if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
jaroslav@1258
  1047
            millis++;
jaroslav@1258
  1048
        }
jaroslav@1258
  1049
jaroslav@1258
  1050
        join(millis);
jaroslav@1258
  1051
    }
jaroslav@1258
  1052
jaroslav@1258
  1053
    /**
jaroslav@1258
  1054
     * Waits for this thread to die.
jaroslav@1258
  1055
     *
jaroslav@1258
  1056
     * <p> An invocation of this method behaves in exactly the same
jaroslav@1258
  1057
     * way as the invocation
jaroslav@1258
  1058
     *
jaroslav@1258
  1059
     * <blockquote>
jaroslav@1258
  1060
     * {@linkplain #join(long) join}{@code (0)}
jaroslav@1258
  1061
     * </blockquote>
jaroslav@1258
  1062
     *
jaroslav@1258
  1063
     * @throws  InterruptedException
jaroslav@1258
  1064
     *          if any thread has interrupted the current thread. The
jaroslav@1258
  1065
     *          <i>interrupted status</i> of the current thread is
jaroslav@1258
  1066
     *          cleared when this exception is thrown.
jaroslav@1258
  1067
     */
jaroslav@1258
  1068
    public final void join() throws InterruptedException {
jaroslav@1258
  1069
        join(0);
jaroslav@1258
  1070
    }
jaroslav@1258
  1071
jaroslav@1258
  1072
    /**
jaroslav@1258
  1073
     * Prints a stack trace of the current thread to the standard error stream.
jaroslav@1258
  1074
     * This method is used only for debugging.
jaroslav@1258
  1075
     *
jaroslav@1258
  1076
     * @see     Throwable#printStackTrace()
jaroslav@1258
  1077
     */
jaroslav@1258
  1078
    public static void dumpStack() {
jaroslav@1258
  1079
        new Exception("Stack trace").printStackTrace();
jaroslav@1258
  1080
    }
jaroslav@1258
  1081
jaroslav@1258
  1082
    /**
jaroslav@1258
  1083
     * Marks this thread as either a {@linkplain #isDaemon daemon} thread
jaroslav@1258
  1084
     * or a user thread. The Java Virtual Machine exits when the only
jaroslav@1258
  1085
     * threads running are all daemon threads.
jaroslav@1258
  1086
     *
jaroslav@1258
  1087
     * <p> This method must be invoked before the thread is started.
jaroslav@1258
  1088
     *
jaroslav@1258
  1089
     * @param  on
jaroslav@1258
  1090
     *         if {@code true}, marks this thread as a daemon thread
jaroslav@1258
  1091
     *
jaroslav@1258
  1092
     * @throws  IllegalThreadStateException
jaroslav@1258
  1093
     *          if this thread is {@linkplain #isAlive alive}
jaroslav@1258
  1094
     *
jaroslav@1258
  1095
     * @throws  SecurityException
jaroslav@1258
  1096
     *          if {@link #checkAccess} determines that the current
jaroslav@1258
  1097
     *          thread cannot modify this thread
jaroslav@1258
  1098
     */
jaroslav@1258
  1099
    public final void setDaemon(boolean on) {
jaroslav@1260
  1100
        throw new SecurityException();
jaroslav@1258
  1101
    }
jaroslav@1258
  1102
jaroslav@1258
  1103
    /**
jaroslav@1258
  1104
     * Tests if this thread is a daemon thread.
jaroslav@1258
  1105
     *
jaroslav@1258
  1106
     * @return  <code>true</code> if this thread is a daemon thread;
jaroslav@1258
  1107
     *          <code>false</code> otherwise.
jaroslav@1258
  1108
     * @see     #setDaemon(boolean)
jaroslav@1258
  1109
     */
jaroslav@1258
  1110
    public final boolean isDaemon() {
jaroslav@1260
  1111
        return false;
jaroslav@1258
  1112
    }
jaroslav@1258
  1113
jaroslav@1258
  1114
    /**
jaroslav@1258
  1115
     * Determines if the currently running thread has permission to
jaroslav@1258
  1116
     * modify this thread.
jaroslav@1258
  1117
     * <p>
jaroslav@1258
  1118
     * If there is a security manager, its <code>checkAccess</code> method
jaroslav@1258
  1119
     * is called with this thread as its argument. This may result in
jaroslav@1258
  1120
     * throwing a <code>SecurityException</code>.
jaroslav@1258
  1121
     *
jaroslav@1258
  1122
     * @exception  SecurityException  if the current thread is not allowed to
jaroslav@1258
  1123
     *               access this thread.
jaroslav@1258
  1124
     * @see        SecurityManager#checkAccess(Thread)
jaroslav@1258
  1125
     */
jaroslav@1258
  1126
    public final void checkAccess() {
jaroslav@1260
  1127
        throw new SecurityException();
jaroslav@1258
  1128
    }
jaroslav@1258
  1129
jaroslav@1258
  1130
    /**
jaroslav@1258
  1131
     * Returns a string representation of this thread, including the
jaroslav@1258
  1132
     * thread's name, priority, and thread group.
jaroslav@1258
  1133
     *
jaroslav@1258
  1134
     * @return  a string representation of this thread.
jaroslav@1258
  1135
     */
jaroslav@1258
  1136
    public String toString() {
jaroslav@1260
  1137
        return "Thread[" + getName() + "," + getPriority() + "," +
jaroslav@1260
  1138
                        "" + "]";
jaroslav@1258
  1139
    }
jaroslav@1258
  1140
jaroslav@1258
  1141
    /**
jaroslav@1258
  1142
     * Returns the context ClassLoader for this Thread. The context
jaroslav@1258
  1143
     * ClassLoader is provided by the creator of the thread for use
jaroslav@1258
  1144
     * by code running in this thread when loading classes and resources.
jaroslav@1258
  1145
     * If not {@linkplain #setContextClassLoader set}, the default is the
jaroslav@1258
  1146
     * ClassLoader context of the parent Thread. The context ClassLoader of the
jaroslav@1258
  1147
     * primordial thread is typically set to the class loader used to load the
jaroslav@1258
  1148
     * application.
jaroslav@1258
  1149
     *
jaroslav@1258
  1150
     * <p>If a security manager is present, and the invoker's class loader is not
jaroslav@1258
  1151
     * {@code null} and is not the same as or an ancestor of the context class
jaroslav@1258
  1152
     * loader, then this method invokes the security manager's {@link
jaroslav@1258
  1153
     * SecurityManager#checkPermission(java.security.Permission) checkPermission}
jaroslav@1258
  1154
     * method with a {@link RuntimePermission RuntimePermission}{@code
jaroslav@1258
  1155
     * ("getClassLoader")} permission to verify that retrieval of the context
jaroslav@1258
  1156
     * class loader is permitted.
jaroslav@1258
  1157
     *
jaroslav@1258
  1158
     * @return  the context ClassLoader for this Thread, or {@code null}
jaroslav@1258
  1159
     *          indicating the system class loader (or, failing that, the
jaroslav@1258
  1160
     *          bootstrap class loader)
jaroslav@1258
  1161
     *
jaroslav@1258
  1162
     * @throws  SecurityException
jaroslav@1258
  1163
     *          if the current thread cannot get the context ClassLoader
jaroslav@1258
  1164
     *
jaroslav@1258
  1165
     * @since 1.2
jaroslav@1258
  1166
     */
jaroslav@1258
  1167
    public ClassLoader getContextClassLoader() {
jaroslav@1403
  1168
        return ClassLoader.getSystemClassLoader();
jaroslav@1258
  1169
    }
jaroslav@1258
  1170
jaroslav@1258
  1171
    /**
jaroslav@1258
  1172
     * Sets the context ClassLoader for this Thread. The context
jaroslav@1258
  1173
     * ClassLoader can be set when a thread is created, and allows
jaroslav@1258
  1174
     * the creator of the thread to provide the appropriate class loader,
jaroslav@1258
  1175
     * through {@code getContextClassLoader}, to code running in the thread
jaroslav@1258
  1176
     * when loading classes and resources.
jaroslav@1258
  1177
     *
jaroslav@1258
  1178
     * <p>If a security manager is present, its {@link
jaroslav@1258
  1179
     * SecurityManager#checkPermission(java.security.Permission) checkPermission}
jaroslav@1258
  1180
     * method is invoked with a {@link RuntimePermission RuntimePermission}{@code
jaroslav@1258
  1181
     * ("setContextClassLoader")} permission to see if setting the context
jaroslav@1258
  1182
     * ClassLoader is permitted.
jaroslav@1258
  1183
     *
jaroslav@1258
  1184
     * @param  cl
jaroslav@1258
  1185
     *         the context ClassLoader for this Thread, or null  indicating the
jaroslav@1258
  1186
     *         system class loader (or, failing that, the bootstrap class loader)
jaroslav@1258
  1187
     *
jaroslav@1258
  1188
     * @throws  SecurityException
jaroslav@1258
  1189
     *          if the current thread cannot set the context ClassLoader
jaroslav@1258
  1190
     *
jaroslav@1258
  1191
     * @since 1.2
jaroslav@1258
  1192
     */
jaroslav@1258
  1193
    public void setContextClassLoader(ClassLoader cl) {
jaroslav@1403
  1194
        if (cl == ClassLoader.getSystemClassLoader()) {
jaroslav@1403
  1195
            return;
jaroslav@1403
  1196
        }
jaroslav@1260
  1197
        throw new SecurityException();
jaroslav@1258
  1198
    }
jaroslav@1258
  1199
jaroslav@1258
  1200
    /**
jaroslav@1258
  1201
     * Returns <tt>true</tt> if and only if the current thread holds the
jaroslav@1258
  1202
     * monitor lock on the specified object.
jaroslav@1258
  1203
     *
jaroslav@1258
  1204
     * <p>This method is designed to allow a program to assert that
jaroslav@1258
  1205
     * the current thread already holds a specified lock:
jaroslav@1258
  1206
     * <pre>
jaroslav@1258
  1207
     *     assert Thread.holdsLock(obj);
jaroslav@1258
  1208
     * </pre>
jaroslav@1258
  1209
     *
jaroslav@1258
  1210
     * @param  obj the object on which to test lock ownership
jaroslav@1258
  1211
     * @throws NullPointerException if obj is <tt>null</tt>
jaroslav@1258
  1212
     * @return <tt>true</tt> if the current thread holds the monitor lock on
jaroslav@1258
  1213
     *         the specified object.
jaroslav@1258
  1214
     * @since 1.4
jaroslav@1258
  1215
     */
jaroslav@1260
  1216
    public static boolean holdsLock(Object obj) {
jaroslav@1260
  1217
        return true;
jaroslav@1260
  1218
    }
jaroslav@1258
  1219
jaroslav@1258
  1220
    /**
jaroslav@1258
  1221
     * Returns an array of stack trace elements representing the stack dump
jaroslav@1258
  1222
     * of this thread.  This method will return a zero-length array if
jaroslav@1258
  1223
     * this thread has not started, has started but has not yet been
jaroslav@1258
  1224
     * scheduled to run by the system, or has terminated.
jaroslav@1258
  1225
     * If the returned array is of non-zero length then the first element of
jaroslav@1258
  1226
     * the array represents the top of the stack, which is the most recent
jaroslav@1258
  1227
     * method invocation in the sequence.  The last element of the array
jaroslav@1258
  1228
     * represents the bottom of the stack, which is the least recent method
jaroslav@1258
  1229
     * invocation in the sequence.
jaroslav@1258
  1230
     *
jaroslav@1258
  1231
     * <p>If there is a security manager, and this thread is not
jaroslav@1258
  1232
     * the current thread, then the security manager's
jaroslav@1258
  1233
     * <tt>checkPermission</tt> method is called with a
jaroslav@1258
  1234
     * <tt>RuntimePermission("getStackTrace")</tt> permission
jaroslav@1258
  1235
     * to see if it's ok to get the stack trace.
jaroslav@1258
  1236
     *
jaroslav@1258
  1237
     * <p>Some virtual machines may, under some circumstances, omit one
jaroslav@1258
  1238
     * or more stack frames from the stack trace.  In the extreme case,
jaroslav@1258
  1239
     * a virtual machine that has no stack trace information concerning
jaroslav@1258
  1240
     * this thread is permitted to return a zero-length array from this
jaroslav@1258
  1241
     * method.
jaroslav@1258
  1242
     *
jaroslav@1258
  1243
     * @return an array of <tt>StackTraceElement</tt>,
jaroslav@1258
  1244
     * each represents one stack frame.
jaroslav@1258
  1245
     *
jaroslav@1258
  1246
     * @throws SecurityException
jaroslav@1258
  1247
     *        if a security manager exists and its
jaroslav@1258
  1248
     *        <tt>checkPermission</tt> method doesn't allow
jaroslav@1258
  1249
     *        getting the stack trace of thread.
jaroslav@1258
  1250
     * @see SecurityManager#checkPermission
jaroslav@1258
  1251
     * @see RuntimePermission
jaroslav@1258
  1252
     * @see Throwable#getStackTrace
jaroslav@1258
  1253
     *
jaroslav@1258
  1254
     * @since 1.5
jaroslav@1258
  1255
     */
jaroslav@1258
  1256
    public StackTraceElement[] getStackTrace() {
jaroslav@1260
  1257
        throw new SecurityException();
jaroslav@1258
  1258
    }
jaroslav@1258
  1259
jaroslav@1258
  1260
    /**
jaroslav@1258
  1261
     * Returns a map of stack traces for all live threads.
jaroslav@1258
  1262
     * The map keys are threads and each map value is an array of
jaroslav@1258
  1263
     * <tt>StackTraceElement</tt> that represents the stack dump
jaroslav@1258
  1264
     * of the corresponding <tt>Thread</tt>.
jaroslav@1258
  1265
     * The returned stack traces are in the format specified for
jaroslav@1258
  1266
     * the {@link #getStackTrace getStackTrace} method.
jaroslav@1258
  1267
     *
jaroslav@1258
  1268
     * <p>The threads may be executing while this method is called.
jaroslav@1258
  1269
     * The stack trace of each thread only represents a snapshot and
jaroslav@1258
  1270
     * each stack trace may be obtained at different time.  A zero-length
jaroslav@1258
  1271
     * array will be returned in the map value if the virtual machine has
jaroslav@1258
  1272
     * no stack trace information about a thread.
jaroslav@1258
  1273
     *
jaroslav@1258
  1274
     * <p>If there is a security manager, then the security manager's
jaroslav@1258
  1275
     * <tt>checkPermission</tt> method is called with a
jaroslav@1258
  1276
     * <tt>RuntimePermission("getStackTrace")</tt> permission as well as
jaroslav@1258
  1277
     * <tt>RuntimePermission("modifyThreadGroup")</tt> permission
jaroslav@1258
  1278
     * to see if it is ok to get the stack trace of all threads.
jaroslav@1258
  1279
     *
jaroslav@1258
  1280
     * @return a <tt>Map</tt> from <tt>Thread</tt> to an array of
jaroslav@1258
  1281
     * <tt>StackTraceElement</tt> that represents the stack trace of
jaroslav@1258
  1282
     * the corresponding thread.
jaroslav@1258
  1283
     *
jaroslav@1258
  1284
     * @throws SecurityException
jaroslav@1258
  1285
     *        if a security manager exists and its
jaroslav@1258
  1286
     *        <tt>checkPermission</tt> method doesn't allow
jaroslav@1258
  1287
     *        getting the stack trace of thread.
jaroslav@1258
  1288
     * @see #getStackTrace
jaroslav@1258
  1289
     * @see SecurityManager#checkPermission
jaroslav@1258
  1290
     * @see RuntimePermission
jaroslav@1258
  1291
     * @see Throwable#getStackTrace
jaroslav@1258
  1292
     *
jaroslav@1258
  1293
     * @since 1.5
jaroslav@1258
  1294
     */
jaroslav@1258
  1295
    public static Map<Thread, StackTraceElement[]> getAllStackTraces() {
jaroslav@1260
  1296
        throw new SecurityException();
jaroslav@1258
  1297
    }
jaroslav@1258
  1298
jaroslav@1258
  1299
    /**
jaroslav@1258
  1300
     * Returns the identifier of this Thread.  The thread ID is a positive
jaroslav@1258
  1301
     * <tt>long</tt> number generated when this thread was created.
jaroslav@1258
  1302
     * The thread ID is unique and remains unchanged during its lifetime.
jaroslav@1258
  1303
     * When a thread is terminated, this thread ID may be reused.
jaroslav@1258
  1304
     *
jaroslav@1258
  1305
     * @return this thread's ID.
jaroslav@1258
  1306
     * @since 1.5
jaroslav@1258
  1307
     */
jaroslav@1258
  1308
    public long getId() {
jaroslav@1260
  1309
        return 0;
jaroslav@1258
  1310
    }
jaroslav@1258
  1311
jaroslav@1258
  1312
    /**
jaroslav@1258
  1313
     * A thread state.  A thread can be in one of the following states:
jaroslav@1258
  1314
     * <ul>
jaroslav@1258
  1315
     * <li>{@link #NEW}<br>
jaroslav@1258
  1316
     *     A thread that has not yet started is in this state.
jaroslav@1258
  1317
     *     </li>
jaroslav@1258
  1318
     * <li>{@link #RUNNABLE}<br>
jaroslav@1258
  1319
     *     A thread executing in the Java virtual machine is in this state.
jaroslav@1258
  1320
     *     </li>
jaroslav@1258
  1321
     * <li>{@link #BLOCKED}<br>
jaroslav@1258
  1322
     *     A thread that is blocked waiting for a monitor lock
jaroslav@1258
  1323
     *     is in this state.
jaroslav@1258
  1324
     *     </li>
jaroslav@1258
  1325
     * <li>{@link #WAITING}<br>
jaroslav@1258
  1326
     *     A thread that is waiting indefinitely for another thread to
jaroslav@1258
  1327
     *     perform a particular action is in this state.
jaroslav@1258
  1328
     *     </li>
jaroslav@1258
  1329
     * <li>{@link #TIMED_WAITING}<br>
jaroslav@1258
  1330
     *     A thread that is waiting for another thread to perform an action
jaroslav@1258
  1331
     *     for up to a specified waiting time is in this state.
jaroslav@1258
  1332
     *     </li>
jaroslav@1258
  1333
     * <li>{@link #TERMINATED}<br>
jaroslav@1258
  1334
     *     A thread that has exited is in this state.
jaroslav@1258
  1335
     *     </li>
jaroslav@1258
  1336
     * </ul>
jaroslav@1258
  1337
     *
jaroslav@1258
  1338
     * <p>
jaroslav@1258
  1339
     * A thread can be in only one state at a given point in time.
jaroslav@1258
  1340
     * These states are virtual machine states which do not reflect
jaroslav@1258
  1341
     * any operating system thread states.
jaroslav@1258
  1342
     *
jaroslav@1258
  1343
     * @since   1.5
jaroslav@1258
  1344
     * @see #getState
jaroslav@1258
  1345
     */
jaroslav@1258
  1346
    public enum State {
jaroslav@1258
  1347
        /**
jaroslav@1258
  1348
         * Thread state for a thread which has not yet started.
jaroslav@1258
  1349
         */
jaroslav@1258
  1350
        NEW,
jaroslav@1258
  1351
jaroslav@1258
  1352
        /**
jaroslav@1258
  1353
         * Thread state for a runnable thread.  A thread in the runnable
jaroslav@1258
  1354
         * state is executing in the Java virtual machine but it may
jaroslav@1258
  1355
         * be waiting for other resources from the operating system
jaroslav@1258
  1356
         * such as processor.
jaroslav@1258
  1357
         */
jaroslav@1258
  1358
        RUNNABLE,
jaroslav@1258
  1359
jaroslav@1258
  1360
        /**
jaroslav@1258
  1361
         * Thread state for a thread blocked waiting for a monitor lock.
jaroslav@1258
  1362
         * A thread in the blocked state is waiting for a monitor lock
jaroslav@1258
  1363
         * to enter a synchronized block/method or
jaroslav@1258
  1364
         * reenter a synchronized block/method after calling
jaroslav@1258
  1365
         * {@link Object#wait() Object.wait}.
jaroslav@1258
  1366
         */
jaroslav@1258
  1367
        BLOCKED,
jaroslav@1258
  1368
jaroslav@1258
  1369
        /**
jaroslav@1258
  1370
         * Thread state for a waiting thread.
jaroslav@1258
  1371
         * A thread is in the waiting state due to calling one of the
jaroslav@1258
  1372
         * following methods:
jaroslav@1258
  1373
         * <ul>
jaroslav@1258
  1374
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
jaroslav@1258
  1375
         *   <li>{@link #join() Thread.join} with no timeout</li>
jaroslav@1258
  1376
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
jaroslav@1258
  1377
         * </ul>
jaroslav@1258
  1378
         *
jaroslav@1258
  1379
         * <p>A thread in the waiting state is waiting for another thread to
jaroslav@1258
  1380
         * perform a particular action.
jaroslav@1258
  1381
         *
jaroslav@1258
  1382
         * For example, a thread that has called <tt>Object.wait()</tt>
jaroslav@1258
  1383
         * on an object is waiting for another thread to call
jaroslav@1258
  1384
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
jaroslav@1258
  1385
         * that object. A thread that has called <tt>Thread.join()</tt>
jaroslav@1258
  1386
         * is waiting for a specified thread to terminate.
jaroslav@1258
  1387
         */
jaroslav@1258
  1388
        WAITING,
jaroslav@1258
  1389
jaroslav@1258
  1390
        /**
jaroslav@1258
  1391
         * Thread state for a waiting thread with a specified waiting time.
jaroslav@1258
  1392
         * A thread is in the timed waiting state due to calling one of
jaroslav@1258
  1393
         * the following methods with a specified positive waiting time:
jaroslav@1258
  1394
         * <ul>
jaroslav@1258
  1395
         *   <li>{@link #sleep Thread.sleep}</li>
jaroslav@1258
  1396
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
jaroslav@1258
  1397
         *   <li>{@link #join(long) Thread.join} with timeout</li>
jaroslav@1258
  1398
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
jaroslav@1258
  1399
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
jaroslav@1258
  1400
         * </ul>
jaroslav@1258
  1401
         */
jaroslav@1258
  1402
        TIMED_WAITING,
jaroslav@1258
  1403
jaroslav@1258
  1404
        /**
jaroslav@1258
  1405
         * Thread state for a terminated thread.
jaroslav@1258
  1406
         * The thread has completed execution.
jaroslav@1258
  1407
         */
jaroslav@1258
  1408
        TERMINATED;
jaroslav@1258
  1409
    }
jaroslav@1258
  1410
jaroslav@1258
  1411
    /**
jaroslav@1258
  1412
     * Returns the state of this thread.
jaroslav@1258
  1413
     * This method is designed for use in monitoring of the system state,
jaroslav@1258
  1414
     * not for synchronization control.
jaroslav@1258
  1415
     *
jaroslav@1258
  1416
     * @return this thread's state.
jaroslav@1258
  1417
     * @since 1.5
jaroslav@1258
  1418
     */
jaroslav@1258
  1419
    public State getState() {
jaroslav@1258
  1420
        // get current thread state
jaroslav@1260
  1421
        return State.RUNNABLE;
jaroslav@1258
  1422
    }
jaroslav@1258
  1423
jaroslav@1258
  1424
    // Added in JSR-166
jaroslav@1258
  1425
jaroslav@1258
  1426
    /**
jaroslav@1258
  1427
     * Interface for handlers invoked when a <tt>Thread</tt> abruptly
jaroslav@1258
  1428
     * terminates due to an uncaught exception.
jaroslav@1258
  1429
     * <p>When a thread is about to terminate due to an uncaught exception
jaroslav@1258
  1430
     * the Java Virtual Machine will query the thread for its
jaroslav@1258
  1431
     * <tt>UncaughtExceptionHandler</tt> using
jaroslav@1258
  1432
     * {@link #getUncaughtExceptionHandler} and will invoke the handler's
jaroslav@1258
  1433
     * <tt>uncaughtException</tt> method, passing the thread and the
jaroslav@1258
  1434
     * exception as arguments.
jaroslav@1258
  1435
     * If a thread has not had its <tt>UncaughtExceptionHandler</tt>
jaroslav@1258
  1436
     * explicitly set, then its <tt>ThreadGroup</tt> object acts as its
jaroslav@1258
  1437
     * <tt>UncaughtExceptionHandler</tt>. If the <tt>ThreadGroup</tt> object
jaroslav@1258
  1438
     * has no
jaroslav@1258
  1439
     * special requirements for dealing with the exception, it can forward
jaroslav@1258
  1440
     * the invocation to the {@linkplain #getDefaultUncaughtExceptionHandler
jaroslav@1258
  1441
     * default uncaught exception handler}.
jaroslav@1258
  1442
     *
jaroslav@1258
  1443
     * @see #setDefaultUncaughtExceptionHandler
jaroslav@1258
  1444
     * @see #setUncaughtExceptionHandler
jaroslav@1258
  1445
     * @see ThreadGroup#uncaughtException
jaroslav@1258
  1446
     * @since 1.5
jaroslav@1258
  1447
     */
jaroslav@1258
  1448
    public interface UncaughtExceptionHandler {
jaroslav@1258
  1449
        /**
jaroslav@1258
  1450
         * Method invoked when the given thread terminates due to the
jaroslav@1258
  1451
         * given uncaught exception.
jaroslav@1258
  1452
         * <p>Any exception thrown by this method will be ignored by the
jaroslav@1258
  1453
         * Java Virtual Machine.
jaroslav@1258
  1454
         * @param t the thread
jaroslav@1258
  1455
         * @param e the exception
jaroslav@1258
  1456
         */
jaroslav@1258
  1457
        void uncaughtException(Thread t, Throwable e);
jaroslav@1258
  1458
    }
jaroslav@1258
  1459
jaroslav@1258
  1460
    // null unless explicitly set
jaroslav@1258
  1461
    private volatile UncaughtExceptionHandler uncaughtExceptionHandler;
jaroslav@1258
  1462
jaroslav@1258
  1463
    // null unless explicitly set
jaroslav@1258
  1464
    private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler;
jaroslav@1258
  1465
jaroslav@1258
  1466
    /**
jaroslav@1258
  1467
     * Set the default handler invoked when a thread abruptly terminates
jaroslav@1258
  1468
     * due to an uncaught exception, and no other handler has been defined
jaroslav@1258
  1469
     * for that thread.
jaroslav@1258
  1470
     *
jaroslav@1258
  1471
     * <p>Uncaught exception handling is controlled first by the thread, then
jaroslav@1258
  1472
     * by the thread's {@link ThreadGroup} object and finally by the default
jaroslav@1258
  1473
     * uncaught exception handler. If the thread does not have an explicit
jaroslav@1258
  1474
     * uncaught exception handler set, and the thread's thread group
jaroslav@1258
  1475
     * (including parent thread groups)  does not specialize its
jaroslav@1258
  1476
     * <tt>uncaughtException</tt> method, then the default handler's
jaroslav@1258
  1477
     * <tt>uncaughtException</tt> method will be invoked.
jaroslav@1258
  1478
     * <p>By setting the default uncaught exception handler, an application
jaroslav@1258
  1479
     * can change the way in which uncaught exceptions are handled (such as
jaroslav@1258
  1480
     * logging to a specific device, or file) for those threads that would
jaroslav@1258
  1481
     * already accept whatever &quot;default&quot; behavior the system
jaroslav@1258
  1482
     * provided.
jaroslav@1258
  1483
     *
jaroslav@1258
  1484
     * <p>Note that the default uncaught exception handler should not usually
jaroslav@1258
  1485
     * defer to the thread's <tt>ThreadGroup</tt> object, as that could cause
jaroslav@1258
  1486
     * infinite recursion.
jaroslav@1258
  1487
     *
jaroslav@1258
  1488
     * @param eh the object to use as the default uncaught exception handler.
jaroslav@1258
  1489
     * If <tt>null</tt> then there is no default handler.
jaroslav@1258
  1490
     *
jaroslav@1258
  1491
     * @throws SecurityException if a security manager is present and it
jaroslav@1258
  1492
     *         denies <tt>{@link RuntimePermission}
jaroslav@1258
  1493
     *         (&quot;setDefaultUncaughtExceptionHandler&quot;)</tt>
jaroslav@1258
  1494
     *
jaroslav@1258
  1495
     * @see #setUncaughtExceptionHandler
jaroslav@1258
  1496
     * @see #getUncaughtExceptionHandler
jaroslav@1258
  1497
     * @see ThreadGroup#uncaughtException
jaroslav@1258
  1498
     * @since 1.5
jaroslav@1258
  1499
     */
jaroslav@1258
  1500
    public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
jaroslav@1260
  1501
        throw new SecurityException();
jaroslav@1260
  1502
    }
jaroslav@1258
  1503
jaroslav@1258
  1504
    /**
jaroslav@1258
  1505
     * Returns the default handler invoked when a thread abruptly terminates
jaroslav@1258
  1506
     * due to an uncaught exception. If the returned value is <tt>null</tt>,
jaroslav@1258
  1507
     * there is no default.
jaroslav@1258
  1508
     * @since 1.5
jaroslav@1258
  1509
     * @see #setDefaultUncaughtExceptionHandler
jaroslav@1258
  1510
     */
jaroslav@1258
  1511
    public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){
jaroslav@1258
  1512
        return defaultUncaughtExceptionHandler;
jaroslav@1258
  1513
    }
jaroslav@1258
  1514
jaroslav@1258
  1515
    /**
jaroslav@1258
  1516
     * Returns the handler invoked when this thread abruptly terminates
jaroslav@1258
  1517
     * due to an uncaught exception. If this thread has not had an
jaroslav@1258
  1518
     * uncaught exception handler explicitly set then this thread's
jaroslav@1258
  1519
     * <tt>ThreadGroup</tt> object is returned, unless this thread
jaroslav@1258
  1520
     * has terminated, in which case <tt>null</tt> is returned.
jaroslav@1258
  1521
     * @since 1.5
jaroslav@1258
  1522
     */
jaroslav@1258
  1523
    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
jaroslav@1258
  1524
        return uncaughtExceptionHandler != null ?
jaroslav@1260
  1525
            uncaughtExceptionHandler : null;
jaroslav@1258
  1526
    }
jaroslav@1258
  1527
jaroslav@1258
  1528
    /**
jaroslav@1258
  1529
     * Set the handler invoked when this thread abruptly terminates
jaroslav@1258
  1530
     * due to an uncaught exception.
jaroslav@1258
  1531
     * <p>A thread can take full control of how it responds to uncaught
jaroslav@1258
  1532
     * exceptions by having its uncaught exception handler explicitly set.
jaroslav@1258
  1533
     * If no such handler is set then the thread's <tt>ThreadGroup</tt>
jaroslav@1258
  1534
     * object acts as its handler.
jaroslav@1258
  1535
     * @param eh the object to use as this thread's uncaught exception
jaroslav@1258
  1536
     * handler. If <tt>null</tt> then this thread has no explicit handler.
jaroslav@1258
  1537
     * @throws  SecurityException  if the current thread is not allowed to
jaroslav@1258
  1538
     *          modify this thread.
jaroslav@1258
  1539
     * @see #setDefaultUncaughtExceptionHandler
jaroslav@1258
  1540
     * @see ThreadGroup#uncaughtException
jaroslav@1258
  1541
     * @since 1.5
jaroslav@1258
  1542
     */
jaroslav@1258
  1543
    public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
jaroslav@1258
  1544
        checkAccess();
jaroslav@1258
  1545
        uncaughtExceptionHandler = eh;
jaroslav@1258
  1546
    }
jaroslav@1258
  1547
}