home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / Thread.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  35.4 KB  |  994 lines

  1. /*
  2.  * @(#)Thread.java    1.83 98/03/18
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. import java.security.AccessController;
  18. import java.security.AccessControlContext;
  19.  
  20. /**
  21.  * A <i>thread</i> is a thread of execution in a program. The Java 
  22.  * Virtual Machine allows an application to have multiple threads of 
  23.  * execution running concurrently. 
  24.  * <p>
  25.  * Every thread has a priority. Threads with higher priority are 
  26.  * executed in preference to threads with lower priority. Each thread 
  27.  * may or may not also be marked as a daemon. When code running in 
  28.  * some thread creates a new <code>Thread</code> object, the new 
  29.  * thread has its priority initially set equal to the priority of the 
  30.  * creating thread, and is a daemon thread if and only if the 
  31.  * creating thread is a daemon. 
  32.  * <p>
  33.  * When a Java Virtual Machine starts up, there is usually a single 
  34.  * non-daemon thread (which typically calls the method named 
  35.  * <code>main</code> of some designated class). The Java Virtual 
  36.  * Machine continues to execute threads until either of the following 
  37.  * occurs: 
  38.  * <ul>
  39.  * <li>The <code>exit</code> method of class <code>Runtime</code> has been 
  40.  *     called and the security manager has permitted the exit operation 
  41.  *     to take place. 
  42.  * <li>All threads that are not daemon threads have died, either by 
  43.  *     returning from the call to the <code>run</code> method or by 
  44.  *     throwing an exception that propagates beyond the <code>run</code>
  45.  *     method.
  46.  * </ul>
  47.  * <p>
  48.  * There are two ways to create a new thread of execution. One is to 
  49.  * declare a class to be a subclass of <code>Thread</code>. This 
  50.  * subclass should override the <code>run</code> method of class 
  51.  * <code>Thread</code>. An instance of the subclass can then be 
  52.  * allocated and started. For example, a thread that computes primes 
  53.  * larger than a stated value could be written as follows: 
  54.  * <p><hr><blockquote><pre>
  55.  *     class PrimeThread extends Thread {
  56.  *         long minPrime;
  57.  *         PrimeThread(long minPrime) {
  58.  *             this.minPrime = minPrime;
  59.  *         }
  60.  * 
  61.  *         public void run() {
  62.  *             // compute primes larger than minPrime
  63.  *              . . .
  64.  *         }
  65.  *     }
  66.  * </pre></blockquote><hr>
  67.  * <p>
  68.  * The following code would then create a thread and start it running: 
  69.  * <p><blockquote><pre>
  70.  *     PrimeThread p = new PrimeThread(143);
  71.  *     p.start();
  72.  * </pre></blockquote>
  73.  * <p>
  74.  * The other way to create a thread is to declare a class that 
  75.  * implements the <code>Runnable</code> interface. That class then 
  76.  * implements the <code>run</code> method. An instance of the class can 
  77.  * then be allocated, passed as an argument when creating 
  78.  * <code>Thread</code>, and started. The same example in this other 
  79.  * style looks like the following: 
  80.  * <p><hr><blockquote><pre>
  81.  *     class PrimeRun implements Runnable {
  82.  *         long minPrime;
  83.  *         PrimeRun(long minPrime) {
  84.  *             this.minPrime = minPrime;
  85.  *         }
  86.  * 
  87.  *         public void run() {
  88.  *             // compute primes larger than minPrime
  89.  *              . . .
  90.  *         }
  91.  *     }
  92.  * </pre></blockquote><hr>
  93.  * <p>
  94.  * The following code would then create a thread and start it running: 
  95.  * <p><blockquote><pre>
  96.  *     PrimeRun p = new PrimeRun(143);
  97.  *     new Thread(p).start();
  98.  * </pre></blockquote>
  99.  * <p>
  100.  * Every thread has a name for identification purposes. More than 
  101.  * one thread may have the same name. If a name is not specified when 
  102.  * a thread is created, a new name is generated for it. 
  103.  *
  104.  * @author  unascribed
  105.  * @version 1.83, 03/18/98
  106.  * @see     java.lang.Runnable
  107.  * @see     java.lang.Runtime#exit(int)
  108.  * @see     java.lang.Thread#run()
  109.  * @see     java.lang.Thread#stop()
  110.  * @since   JDK1.0
  111.  */
  112. public
  113. class Thread implements Runnable {
  114.     /* Make sure registerNatives is the first thing <clinit> does. */
  115.     private static native void registerNatives();
  116.     static {
  117.         registerNatives();
  118.     }
  119.  
  120.     private char    name[];
  121.     private int         priority;
  122.     private Thread    threadQ;
  123.     private long    eetop;
  124.  
  125.     /* Whether or not to single_step this thread. */
  126.     private boolean    single_step;
  127.  
  128.     /* Whether or not the thread is a daemon thread. */
  129.     private boolean    daemon = false;
  130.  
  131.     /* Whether or not this thread was asked to exit before it runs.*/
  132.     private boolean    stillborn = false;
  133.  
  134.     /* What will be run. */
  135.     private Runnable target;
  136.  
  137.     /* The system queue of threads is linked through activeThreadQueue. */
  138.     private static Thread activeThreadQ;
  139.  
  140.     /* The group of this thread */
  141.     private ThreadGroup    group;
  142.  
  143.     /* The context ClassLoader for this thread */
  144.     private ClassLoader contextClassLoader;
  145.  
  146.     /* The inherited AccessControlContext of this thread */
  147.     private AccessControlContext inheritedAccessControlContext;
  148.  
  149.     /* For autonumbering anonymous threads. */
  150.     private static int threadInitNumber;
  151.     private static synchronized int nextThreadNum() {
  152.     return threadInitNumber++;
  153.     }
  154.  
  155.     /**
  156.      * The minimum priority that a thread can have. 
  157.      */
  158.     public final static int MIN_PRIORITY = 1;
  159.  
  160.    /**
  161.      * The default priority that is assigned to a thread. 
  162.      */
  163.     public final static int NORM_PRIORITY = 5;
  164.  
  165.     /**
  166.      * The maximum priority that a thread can have. 
  167.      */
  168.     public final static int MAX_PRIORITY = 10;
  169.  
  170.     /**
  171.      * Returns a reference to the currently executing thread object.
  172.      *
  173.      * @return  the currently executing thread.
  174.      */
  175.     public static native Thread currentThread();
  176.  
  177.     /**
  178.      * Causes the currently executing thread object to temporarily pause 
  179.      * and allow other threads to execute. 
  180.      */
  181.     public static native void yield();
  182.  
  183.     /**    
  184.      * Causes the currently executing thread to sleep (temporarily cease 
  185.      * execution) for the specified number of milliseconds. The thread 
  186.      * does not lose ownership of any monitors.
  187.      *
  188.      * @param      millis   the length of time to sleep in milliseconds.
  189.      * @exception  InterruptedException  if another thread has interrupted
  190.      *               this thread.
  191.      * @see        java.lang.Object#notify()
  192.      */
  193.     public static native void sleep(long millis) throws InterruptedException;
  194.  
  195.     /**
  196.      * Causes the currently executing thread to sleep (cease execution) 
  197.      * for the specified number of milliseconds plus the specified number 
  198.      * of nanoseconds. The thread does not lose ownership of any monitors.
  199.      *
  200.      * @param      millis   the length of time to sleep in milliseconds.
  201.      * @param      nanos    0-999999 additional nanoseconds to sleep.
  202.      * @exception  IllegalArgumentException  if the value of millis is 
  203.      *             negative or the value of nanos is not in the range 
  204.      *             0-999999.
  205.      * @exception  InterruptedException  if another thread has interrupted
  206.      *               this thread.
  207.      * @see        java.lang.Object#notify()
  208.      */
  209.     public static void sleep(long millis, int nanos) 
  210.     throws InterruptedException {
  211.     if (millis < 0) {
  212.             throw new IllegalArgumentException("timeout value is negative");
  213.     }
  214.  
  215.     if (nanos < 0 || nanos > 999999) {
  216.             throw new IllegalArgumentException(
  217.                 "nanosecond timeout value out of range");
  218.     }
  219.  
  220.     if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
  221.         millis++;
  222.     }
  223.  
  224.     sleep(millis);
  225.     }
  226.  
  227.     /**
  228.      * Initialize a Thread.
  229.      *
  230.      * @param g the Thread group
  231.      * @param target the object whose run() method gets called
  232.      * @param name the name of the new Thread
  233.      */
  234.     private void init(ThreadGroup g, Runnable target, String name){
  235.     Thread parent = currentThread();
  236.     if (g == null) {
  237.         /* Determine if it's an applet or not */
  238.         SecurityManager security = System.getSecurityManager();
  239.         
  240.         /* If there is a security manager, ask the security manager
  241.            what to do. */
  242.         if (security != null) {
  243.         g = security.getThreadGroup();
  244.         }
  245.  
  246.         /* If the security doesn't have a strong opinion of the matter
  247.            use the parent thread group. */
  248.         if (g == null) {
  249.         g = parent.getThreadGroup();
  250.         }
  251.     }
  252.  
  253.     /* checkAccess regardless of whether or not threadgroup is
  254.            explicitly passed in. */
  255.     g.checkAccess();        
  256.  
  257.     this.group = g;
  258.     this.daemon = parent.isDaemon();
  259.     this.priority = parent.getPriority();
  260.     this.name = name.toCharArray();
  261.     this.contextClassLoader = parent.getContextClassLoader();
  262.     this.inheritedAccessControlContext = AccessController.getContext();
  263.     this.target = target;
  264.     setPriority(priority);
  265.     g.add(this);
  266.     }
  267.  
  268.    /**
  269.      * Allocates a new <code>Thread</code> object. This constructor has 
  270.      * the same effect as <code>Thread(null, null,</code>
  271.      * <i>gname</i><code>)</code>, where <b><i>gname</i></b> is 
  272.      * a newly generated name. Automatically generated names are of the 
  273.      * form <code>"Thread-"+</code><i>n</i>, where <i>n</i> is an integer. 
  274.      * <p>
  275.      * Threads created this way must have overridden their
  276.      * <code>run()</code> method to actually do anything.  An example
  277.      * illustrating this method being used follows:
  278.      * <p><blockquote><pre>
  279.      *     import java.lang.*;
  280.      *
  281.      *     class plain01 implements Runnable {
  282.      *         String name; 
  283.      *         plain01() {
  284.      *             name = null;
  285.      *         }
  286.      *         plain01(String s) {
  287.      *             name = s;
  288.      *         }
  289.      *         public void run() {
  290.      *             if (name == null)
  291.      *                 System.out.println("A new thread created");
  292.      *             else
  293.      *                 System.out.println("A new thread with name " + name +
  294.      *                                    " created");
  295.      *         }
  296.      *     }
  297.      *     class threadtest01 {
  298.      *         public static void main(String args[] ) {
  299.      *             int failed = 0 ;
  300.      *
  301.      *             <b>Thread t1 = new Thread();</b>  
  302.      *             if (t1 != null)
  303.      *                 System.out.println("new Thread() succeed");
  304.      *             else {
  305.      *                 System.out.println("new Thread() failed"); 
  306.      *                 failed++; 
  307.      *             }
  308.      *         }
  309.      *     }
  310.      * </pre></blockquote>
  311.      *
  312.      * @see     java.lang.Thread#Thread(java.lang.ThreadGroup,
  313.      *          java.lang.Runnable, java.lang.String)
  314.      */
  315.     public Thread() {
  316.     init(null, null, "Thread-" + nextThreadNum());
  317.     }
  318.  
  319.     /**
  320.      * Allocates a new <code>Thread</code> object. This constructor has 
  321.      * the same effect as <code>Thread(null, target,</code>
  322.      * <i>gname</i><code>)</code>, where <i>gname</i> is 
  323.      * a newly generated name. Automatically generated names are of the 
  324.      * form <code>"Thread-"+</code><i>n</i>, where <i>n</i> is an integer. 
  325.      *
  326.      * @param   target   the object whose <code>run</code> method is called.
  327.      * @see     java.lang.Thread#Thread(java.lang.ThreadGroup, 
  328.      *          java.lang.Runnable, java.lang.String)
  329.      */
  330.     public Thread(Runnable target) {
  331.     init(null, target, "Thread-" + nextThreadNum());
  332.     }
  333.  
  334.     /**
  335.      * Allocates a new <code>Thread</code> object. This constructor has 
  336.      * the same effect as <code>Thread(group, target,</code>
  337.      * <i>gname</i><code>)</code>, where <i>gname</i> is 
  338.      * a newly generated name. Automatically generated names are of the 
  339.      * form <code>"Thread-"+</code><i>n</i>, where <i>n</i> is an integer. 
  340.      *
  341.      * @param      group    the thread group.
  342.      * @param      target   the object whose <code>run</code> method is called.
  343.      * @exception  SecurityException  if the current thread cannot create a
  344.      *             thread in the specified thread group.
  345.      * @see        java.lang.Thread#Thread(java.lang.ThreadGroup, 
  346.      *             java.lang.Runnable, java.lang.String)
  347.      */
  348.     public Thread(ThreadGroup group, Runnable target) {
  349.     init(group, target, "Thread-" + nextThreadNum());
  350.     }
  351.  
  352.     /**
  353.      * Allocates a new <code>Thread</code> object. This constructor has 
  354.      * the same effect as <code>Thread(null, null, name)</code>. 
  355.      *
  356.      * @param   name   the name of the new thread.
  357.      * @see     java.lang.Thread#Thread(java.lang.ThreadGroup, 
  358.      *          java.lang.Runnable, java.lang.String)
  359.      */
  360.     public Thread(String name) {
  361.     init(null, null, name);
  362.     }
  363.  
  364.     /**
  365.      * Allocates a new <code>Thread</code> object. This constructor has 
  366.      * the same effect as <code>Thread(group, null, name)</code> 
  367.      *
  368.      * @param      group   the thread group.
  369.      * @param      name    the name of the new thread.
  370.      * @exception  SecurityException  if the current thread cannot create a
  371.      *               thread in the specified thread group.
  372.      * @see        java.lang.Thread#Thread(java.lang.ThreadGroup, 
  373.      *          java.lang.Runnable, java.lang.String)
  374.      */
  375.     public Thread(ThreadGroup group, String name) {
  376.     init(group, null, name);
  377.     }
  378.  
  379.     /**
  380.      * Allocates a new <code>Thread</code> object. This constructor has 
  381.      * the same effect as <code>Thread(null, target, name)</code>. 
  382.      *
  383.      * @param   target   the object whose <code>run</code> method is called.
  384.      * @param   name     the name of the new thread.
  385.      * @see     java.lang.Thread#Thread(java.lang.ThreadGroup, 
  386.      *          java.lang.Runnable, java.lang.String)
  387.      */
  388.     public Thread(Runnable target, String name) {
  389.     init(null, target, name);
  390.     }
  391.  
  392.     /**
  393.      * Allocates a new <code>Thread</code> object so that it has 
  394.      * <code>target</code> as its run object, has the specified 
  395.      * <code>name</code> as its name, and belongs to the thread group 
  396.      * referred to by <code>group</code>.
  397.      * <p>
  398.      * If <code>group</code> is not <code>null</code>, the 
  399.      * <code>checkAccess</code> method of that thread group is called with 
  400.      * no arguments; this may result in throwing a 
  401.      * <code>SecurityException</code>; if <code>group</code> is 
  402.      * <code>null</code>, the new process belongs to the same group as 
  403.      * the thread that is creating the new thread. 
  404.      * <p>
  405.      * If the <code>target</code> argument is not <code>null</code>, the 
  406.      * <code>run</code> method of the <code>target</code> is called when 
  407.      * this thread is started. If the target argument is 
  408.      * <code>null</code>, this thread's <code>run</code> method is called 
  409.      * when this thread is started. 
  410.      * <p>
  411.      * The priority of the newly created thread is set equal to the 
  412.      * priority of the thread creating it, that is, the currently running 
  413.      * thread. The method <code>setPriority</code> may be used to 
  414.      * change the priority to a new value. 
  415.      * <p>
  416.      * The newly created thread is initially marked as being a daemon 
  417.      * thread if and only if the thread creating it is currently marked 
  418.      * as a daemon thread. The method <code>setDaemon </code> may be used 
  419.      * to change whether or not a thread is a daemon. 
  420.      *
  421.      * @param      group     the thread group.
  422.      * @param      target   the object whose <code>run</code> method is called.
  423.      * @param      name     the name of the new thread.
  424.      * @exception  SecurityException  if the current thread cannot create a
  425.      *               thread in the specified thread group.
  426.      * @see        java.lang.Runnable#run()
  427.      * @see        java.lang.Thread#run()
  428.      * @see        java.lang.Thread#setDaemon(boolean)
  429.      * @see        java.lang.Thread#setPriority(int)
  430.      * @see        java.lang.ThreadGroup#checkAccess()
  431.      */
  432.     public Thread(ThreadGroup group, Runnable target, String name) {
  433.     init(group, target, name);
  434.     }
  435.  
  436.     /**
  437.      * Causes this thread to begin execution; the Java Virtual Machine 
  438.      * calls the <code>run</code> method of this thread. 
  439.      * <p>
  440.      * The result is that two threads are running concurrently: the 
  441.      * current thread (which returns from the call to the 
  442.      * <code>start</code> method) and the other thread (which executes its 
  443.      * <code>run</code> method). 
  444.      *
  445.      * @exception  IllegalThreadStateException  if the thread was already
  446.      *               started.
  447.      * @see        java.lang.Thread#run()
  448.      * @see        java.lang.Thread#stop()
  449.      */
  450.     public synchronized native void start();
  451.  
  452.     /**
  453.      * If this thread was constructed using a separate 
  454.      * <code>Runnable</code> run object, then that 
  455.      * <code>Runnable</code> object's <code>run</code> method is called; 
  456.      * otherwise, this method does nothing and returns. 
  457.      * <p>
  458.      * Subclasses of <code>Thread</code> should override this method. 
  459.      *
  460.      * @see     java.lang.Thread#start()
  461.      * @see     java.lang.Thread#stop()
  462.      * @see     java.lang.Thread#Thread(java.lang.ThreadGroup, 
  463.      *          java.lang.Runnable, java.lang.String)
  464.      * @see     java.lang.Runnable#run()
  465.      */
  466.     public void run() {
  467.     if (target != null) {
  468.         target.run();
  469.     }
  470.     }
  471.  
  472.     /**
  473.      * This method is called by the system to give a Thread
  474.      * a chance to clean up before it actually exits.
  475.      */
  476.     private void exit() {
  477.     if (group != null) {
  478.         group.remove(this);
  479.         group = null;
  480.     }
  481.     /* Aggressively null object connected to Thread: see bug 4006245 */
  482.     target = null;
  483.     }
  484.  
  485.     /** 
  486.      * Forces the thread to stop executing.  This method has been deprecated,
  487.      * as it is inherently unsafe.  Stopping a thread with Thread.stop causes
  488.      * it to unlock all of the monitors that it has locked (as a natural
  489.      * consequence of the unchecked <code>ThreadDeath</code> exception
  490.      * propagating up the stack).  If any of the objects previously protected
  491.      * by these monitors were in an inconsistent state, the damaged objects
  492.      * become visible to other threads, potentially resulting in arbitrary
  493.      * behavior.  Many uses of <code>stop</code> should be replaced by code
  494.      * that simply modifies some variable to indicate that the target thread
  495.      * should stop running.  The target thread should check this variable
  496.      * regularly, and return from its run method in an orderly fashion if the
  497.      * variable indicates that it is to stop running.  If the target thread
  498.      * waits for long periods (e.g., for input), the <code>interrupt</code>
  499.      * method should be used to interrupt the wait.
  500.      * <p>
  501.      * First, the <code>checkAccess</code> method of this thread is called 
  502.      * with no arguments. This may result in throwing a 
  503.      * <code>SecurityException</code> (in the current thread). 
  504.      * <p>
  505.      * The thread represented by this thread is forced to stop whatever 
  506.      * it is doing abnormally and to throw a newly created 
  507.      * <code>ThreadDeath</code> object as an exception. 
  508.      * <p>
  509.      * It is permitted to stop a thread that has not yet been started. 
  510.      * If the thread is eventually started, it immediately terminates. 
  511.      * <p>
  512.      * An application should not normally try to catch 
  513.      * <code>ThreadDeath</code> unless it must do some extraordinary 
  514.      * cleanup operation (note that the throwing of 
  515.      * <code>ThreadDeath</code> causes <code>finally</code> clauses of 
  516.      * <code>try</code> statements to be executed before the thread 
  517.      * officially dies).  If a <code>catch</code> clause catches a 
  518.      * <code>ThreadDeath</code> object, it is important to rethrow the 
  519.      * object so that the thread actually dies. 
  520.      * <p>
  521.      * The top-level error handler that reacts to otherwise uncaught 
  522.      * exceptions does not print out a message or otherwise notify the 
  523.      * application if the uncaught exception is an instance of 
  524.      * <code>ThreadDeath</code>. 
  525.      *
  526.      * @exception  SecurityException  if the current thread cannot modify
  527.      *               this thread.
  528.      * @see        java.lang.Thread#interrupt()
  529.      * @see        java.lang.Thread#checkAccess()
  530.      * @see        java.lang.Thread#run()
  531.      * @see        java.lang.Thread#start()
  532.      * @see        java.lang.ThreadDeath
  533.      * @see        java.lang.ThreadGroup#uncaughtException(java.lang.Thread,
  534.      *             java.lang.Throwable)
  535.      * @deprecated */
  536.     public final void stop() {
  537.     stop(new ThreadDeath());
  538.     }
  539.  
  540.     /**
  541.      * Forces the thread to stop executing.  This method has been deprecated
  542.      * because it is inherently unsafe.  See the <code>stop()</code> method
  543.      * (with no arguments) for details.  An additional danger of this method
  544.      * is that it may be used to generate exceptions that the target thread
  545.      * is unprepared to handle (including checked exceptions that the thread
  546.      * could not possibly throw, were it not for this method).
  547.      * <p>
  548.      * First, the <code>checkAccess</code> method of this thread is called 
  549.      * with no arguments. This may result in throwing a 
  550.      * <code>SecurityException </code>(in the current thread). 
  551.      * <p>
  552.      * If the argument <code>obj</code> is null, a 
  553.      * <code>NullPointerException</code> is thrown (in the current thread). 
  554.      * <p>
  555.      * The thread represented by this thread is forced to complete 
  556.      * whatever it is doing abnormally and to throw the 
  557.      * <code>Throwable</code> object <code>obj</code> as an exception. This 
  558.      * is an unusual action to take; normally, the <code>stop</code> method 
  559.      * that takes no arguments should be used. 
  560.      * <p>
  561.      * It is permitted to stop a thread that has not yet been started. 
  562.      * If the thread is eventually started, it immediately terminates. 
  563.      *
  564.      * @param      obj   the Throwable object to be thrown.
  565.      * @exception  SecurityException  if the current thread cannot modify
  566.      *               this thread.
  567.      * @see        java.lang.Thread#interrupt()
  568.      * @see        java.lang.Thread#checkAccess()
  569.      * @see        java.lang.Thread#run()
  570.      * @see        java.lang.Thread#start()
  571.      * @see        java.lang.Thread#stop()
  572.      * @deprecated
  573.      */
  574.     public final synchronized void stop(Throwable o) {
  575.     checkAccess();
  576.     resume();    // Wake up thread if it was suspended; no-op otherwise
  577.     stop0(o);
  578.     }
  579.  
  580.     /**
  581.      * Interrupts this thread.
  582.      */
  583.     // Note that this method is not synchronized.  Three reasons for this:
  584.     // 1) It changes the API.
  585.     // 2) It's another place where the system could hang.
  586.     // 3) All we're doing is turning on a one-way bit.  It doesn't matter
  587.     //    exactly when it's done WRT probes via the interrupted() method.
  588.     public void interrupt() {
  589.     checkAccess();
  590.     interrupt0();
  591.     }
  592.  
  593.     /**
  594.      * Tests if the current thread has been interrupted.
  595.      * Note that <code>interrupted</code> is a static method, while 
  596.      * <code>isInterrupted</code> is called on this 
  597.      * <code>Thread</code> instance. 
  598.      *
  599.      * @return  <code>true</code> if the current thread has been interrupted;
  600.      *          <code>false</code> otherwise.
  601.      * @see     java.lang.Thread#isInterrupted()
  602.      */
  603.     public static boolean interrupted() {
  604.     return currentThread().isInterrupted(true);
  605.     }
  606.  
  607.     /**
  608.      * Tests if this thread has been interrupted.
  609.      * Note that <code>isInterrupted</code> 
  610.      * is called on this <code>Thread</code> instance; by 
  611.      * contrast, <code>interrupted</code> is a static method. 
  612.      *
  613.      * @return  <code>true</code> if this thread has been interrupted;
  614.      *          <code>false</code> otherwise.
  615.      * @see     java.lang.Thread#interrupted()
  616.      */
  617.     public boolean isInterrupted() {
  618.     return isInterrupted(false);
  619.     }
  620.  
  621.     /**
  622.      * Tests if some Thread has been interrupted.  The interrupted state
  623.      * is reset or not based on the value of ClearInterrupted that is
  624.      * passed.
  625.      */
  626.     private native boolean isInterrupted(boolean ClearInterrupted);
  627.  
  628.     /**
  629.      * Destroys this thread, without any cleanup. Any monitors it has 
  630.      * locked remain locked. (This method is not implemented.)
  631.      */
  632.     public void destroy() {
  633.     throw new NoSuchMethodError();
  634.     }
  635.  
  636.     /**
  637.      * Tests if this thread is alive. A thread is alive if it has 
  638.      * been started and has not yet died. 
  639.      *
  640.      * @return  <code>true</code> if this thread is alive;
  641.      *          <code>false</code> otherwise.
  642.      */
  643.     public final native boolean isAlive();
  644.  
  645.     /**
  646.      * Suspends this thread. This method has been deprecated, as it is
  647.      * inherently deadlock-prone.  If the target thread holds a lock on the
  648.      * monitor protecting a critical system resource when it is suspended, no
  649.      * thread can access this resource until the target thread is resumed. If
  650.      * the thread that would resume the target thread attempts to lock this
  651.      * monitor prior to calling <code>resume</code>, deadlock results.  Such
  652.      * deadlocks typically manifest themselves as "frozen" processes.
  653.      * <p>
  654.      * First, the <code>checkAccess</code> method of this thread is called 
  655.      * with no arguments. This may result in throwing a 
  656.      * <code>SecurityException </code>(in the current thread). 
  657.      * <p>
  658.      * If the thread is alive, it is suspended and makes no further 
  659.      * progress unless and until it is resumed. 
  660.      *
  661.      * @exception  SecurityException  if the current thread cannot modify
  662.      *               this thread.
  663.      * @deprecated */
  664.     public final void suspend() {
  665.     checkAccess();
  666.     suspend0();
  667.     }
  668.  
  669.     /**
  670.      * Resumes a suspended thread.  This method has been deprecated along
  671.      * with <code>suspend</code>, which is deadlock-prone.  See
  672.      * <code>suspend</code> for details.
  673.      * <p>
  674.      * First, the <code>checkAccess</code> method of this thread is called 
  675.      * with no arguments. This may result in throwing a 
  676.      * <code>SecurityException</code>(in the current thread). 
  677.      * <p>
  678.      * If the thread is alive but suspended, it is resumed and is 
  679.      * permitted to make progress in its execution. 
  680.      *
  681.      * @exception  SecurityException  if the current thread cannot modify this
  682.      *               thread.
  683.      * @see        java.lang.Thread#suspend()
  684.      * @deprecated
  685.      */
  686.     public final void resume() {
  687.     checkAccess();
  688.     resume0();
  689.     }
  690.  
  691.     /**
  692.      * Changes the priority of this thread. 
  693.      * <p>
  694.      * First the <code>checkAccess</code> method of this thread is called 
  695.      * with no arguments. This may result in throwing a 
  696.      * <code>SecurityException</code>. 
  697.      * <p>
  698.      * Otherwise, the priority of this thread is set to the smaller of 
  699.      * the specified <code>newPriority</code> and the maximum permitted 
  700.      * priority of the thread's thread group. 
  701.      *
  702.      * @exception  IllegalArgumentException  If the priority is not in the
  703.      *               range <code>MIN_PRIORITY</code> to
  704.      *               <code>MAX_PRIORITY</code>.
  705.      * @exception  SecurityException  if the current thread cannot modify
  706.      *               this thread.
  707.      * @see        java.lang.Thread#checkAccess()
  708.      * @see        java.lang.Thread#getPriority()
  709.      * @see        java.lang.Thread#getThreadGroup()
  710.      * @see        java.lang.Thread#MAX_PRIORITY
  711.      * @see        java.lang.Thread#MIN_PRIORITY
  712.      * @see        java.lang.ThreadGroup#getMaxPriority()
  713.      */
  714.     public final void setPriority(int newPriority) {
  715.     checkAccess();
  716.     if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
  717.         throw new IllegalArgumentException();
  718.     }
  719.     if (newPriority > group.getMaxPriority()) {
  720.         newPriority = group.getMaxPriority();
  721.     }
  722.     setPriority0(priority = newPriority);
  723.     }
  724.  
  725.     /**
  726.      * Returns this thread's priority.
  727.      *
  728.      * @return  this thread's name.
  729.      * @see     java.lang.Thread#setPriority(int)
  730.      */
  731.     public final int getPriority() {
  732.     return priority;
  733.     }
  734.  
  735.     /**
  736.      * Changes the name of this thread to be equal to the argument 
  737.      * <code>name</code>. 
  738.      * <p>
  739.      * First the <code>checkAccess</code> method of this thread is called 
  740.      * with no arguments. This may result in throwing a 
  741.      * <code>SecurityException</code>. 
  742.      *
  743.      * @param      name   the new name for this thread.
  744.      * @exception  SecurityException  if the current thread cannot modify this
  745.      *               thread.
  746.      * @see        java.lang.Thread#checkAccess()
  747.      * @see        java.lang.Thread#getName()
  748.      */
  749.     public final void setName(String name) {
  750.     checkAccess();
  751.     this.name = name.toCharArray();
  752.     }
  753.  
  754.     /**
  755.      * Returns this thread's name.
  756.      *
  757.      * @return  this thread's name.
  758.      * @see     java.lang.Thread#setName(java.lang.String)
  759.      */
  760.     public final String getName() {
  761.     return String.valueOf(name);
  762.     }
  763.  
  764.     /**
  765.      * Returns this thread's thread group.
  766.      *
  767.      * @return  this thread's thread group.
  768.      */
  769.     public final ThreadGroup getThreadGroup() {
  770.     return group;
  771.     }
  772.  
  773.     /**
  774.      * Returns the current number of active threads in this thread's 
  775.      * thread group.
  776.      *
  777.      * @return  the current number of threads in this thread's thread group.
  778.      */
  779.     public static int activeCount() {
  780.     return currentThread().getThreadGroup().activeCount();
  781.     }
  782.  
  783.     /**
  784.      * Copies into the specified array every active thread in this 
  785.      * this thread's thread group and its subgroups. This method simply 
  786.      * calls the <code>enumerate</code> method of this thread's thread 
  787.      * group with the array argument. 
  788.      *
  789.      * @return  the number of threads put into the array.
  790.      * @see     java.lang.ThreadGroup#enumerate(java.lang.Thread[])
  791.      */
  792.     public static int enumerate(Thread tarray[]) {
  793.     return currentThread().getThreadGroup().enumerate(tarray);
  794.     }
  795.  
  796.     /**
  797.      * Counts the number of stack frames in this thread. The thread must 
  798.      * be suspended. 
  799.      *
  800.      * @return     the number of stack frames in this thread.
  801.      * @exception  IllegalThreadStateException  if this thread is not
  802.      *             suspended.
  803.      */
  804.     public native int countStackFrames();
  805.  
  806.     /**
  807.      * Waits at most <code>millis</code> milliseconds for this thread to 
  808.      * die. A timeout of <code>0</code> means to wait forever. 
  809.      *
  810.      * @param      millis   the time to wait in milliseconds.
  811.      * @exception  InterruptedException  if another thread has interrupted the
  812.      *               current thread.
  813.      */
  814.     public final synchronized void join(long millis) 
  815.     throws InterruptedException {
  816.     long base = System.currentTimeMillis();
  817.     long now = 0;
  818.  
  819.     if (millis < 0) {
  820.             throw new IllegalArgumentException("timeout value is negative");
  821.     }
  822.  
  823.     if (millis == 0) {
  824.         while (isAlive()) {
  825.         wait(0);
  826.         }
  827.     } else {
  828.         while (isAlive()) {
  829.         long delay = millis - now;
  830.         if (delay <= 0) {
  831.             break;
  832.         }
  833.         wait(delay);
  834.         now = System.currentTimeMillis() - base;
  835.         }
  836.     }
  837.     }
  838.  
  839.     /**
  840.      * Waits at most <code>millis</code> milliseconds plus 
  841.      * <code>nanos</code> nanoseconds for this thread to die. 
  842.      *
  843.      * @param      millis   the time to wait in milliseconds.
  844.      * @param      nanos    0-999999 additional nanoseconds to wait.
  845.      * @exception  IllegalArgumentException  if the value of millis is negative
  846.      *               the value of nanos is not in the range 0-999999.
  847.      * @exception  InterruptedException  if another thread has interrupted the
  848.      *               current thread.
  849.      */
  850.     public final synchronized void join(long millis, int nanos) 
  851.     throws InterruptedException {
  852.  
  853.     if (millis < 0) {
  854.             throw new IllegalArgumentException("timeout value is negative");
  855.     }
  856.  
  857.     if (nanos < 0 || nanos > 999999) {
  858.             throw new IllegalArgumentException(
  859.                 "nanosecond timeout value out of range");
  860.     }
  861.  
  862.     if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
  863.         millis++;
  864.     }
  865.  
  866.     join(millis);
  867.     }
  868.  
  869.     /**
  870.      * Waits for this thread to die. 
  871.      *
  872.      * @exception  InterruptedException  if another thread has interrupted the
  873.      *               current thread.
  874.      */
  875.     public final void join() throws InterruptedException {
  876.     join(0);
  877.     }
  878.  
  879.     /**
  880.      * Prints a stack trace of the current thread. This method is used 
  881.      * only for debugging. 
  882.      *
  883.      * @see     java.lang.Throwable#printStackTrace()
  884.      */
  885.     public static void dumpStack() {
  886.     new Exception("Stack trace").printStackTrace();
  887.     }
  888.  
  889.     /**
  890.      * Marks this thread as either a daemon thread or a user thread. The 
  891.      * Java Virtual Machine exits when the only threads running are all 
  892.      * daemon threads. 
  893.      * <p>
  894.      * This method must be called before the thread is started. 
  895.      *
  896.      * @param      on   if <code>true</code>, marks this thread as a
  897.      *                  daemon thread.
  898.      * @exception  IllegalThreadStateException  if this thread is active.
  899.      * @see        java.lang.Thread#isDaemon()
  900.      */
  901.     public final void setDaemon(boolean on) {
  902.     checkAccess();
  903.     if (isAlive()) {
  904.         throw new IllegalThreadStateException();
  905.     }
  906.     daemon = on;
  907.     }
  908.  
  909.     /**
  910.      * Tests if this thread is a daemon thread.
  911.      *
  912.      * @return  <code>true</code> if this thread is a daemon thread;
  913.      *          <code>false</code> otherwise.
  914.      * @see     java.lang.Thread#setDaemon(boolean)
  915.      */
  916.     public final boolean isDaemon() {
  917.     return daemon;
  918.     }
  919.  
  920.     /**
  921.      * Determines if the currently running thread has permission to 
  922.      * modify this thread. 
  923.      * <p>
  924.      * If there is a security manager, its <code>checkAccess</code> method 
  925.      * is called with this thread as its argument. This may result in 
  926.      * throwing a <code>SecurityException</code>. 
  927.      *
  928.      * @exception  SecurityException  if the current thread is not allowed to
  929.      *               access this thread.
  930.      * @see        java.lang.SecurityManager#checkAccess(java.lang.Thread)
  931.      */
  932.     public void checkAccess() {
  933.     SecurityManager security = System.getSecurityManager();
  934.     if (security != null) {
  935.         security.checkAccess(this);
  936.     }
  937.     }
  938.  
  939.     /**
  940.      * Returns a string representation of this thread, including the 
  941.      * thread's name, priority, and thread group.
  942.      *
  943.      * @return  a string representation of this thread.
  944.      */
  945.     public String toString() {
  946.     if (getThreadGroup() != null) {
  947.         return "Thread[" + getName() + "," + getPriority() + "," + 
  948.                     getThreadGroup().getName() + "]";
  949.     } else {
  950.         return "Thread[" + getName() + "," + getPriority() + "," + 
  951.                     "" + "]";
  952.     }
  953.     }
  954.  
  955.     /**    
  956.      * Returns the context ClassLoader for this Thread. The context
  957.      * ClassLoader is provided by the creator of the thread for use
  958.      * by code running in this thread when loading classes and resources.
  959.      * If not set, the default is the ClassLoader context of the parent
  960.      * Thread. The context ClassLoader of the primordial thread is
  961.      * typically set to the class loader used to load the application.
  962.      *
  963.      * @return the context ClassLoader for this Thread
  964.      * @since JDK1.2
  965.      */
  966.     public ClassLoader getContextClassLoader() {
  967.     return contextClassLoader;
  968.     }
  969.  
  970.     /**   
  971.      * Sets the context ClassLoader for this Thread. The context
  972.      * ClassLoader can be set when a thread is created, and allows
  973.      * the creator of the thread to provide the appropriate class loader
  974.      * to code running in the thread when loading classes and resources.
  975.      *
  976.      * @param cl the context ClassLoader for this Thread
  977.      * @exception SecurityException if the caller does not have permission
  978.      *            to modify this Thread
  979.      * @since JDK1.2 
  980.      */
  981.     public void setContextClassLoader(ClassLoader cl) {
  982.     checkAccess();
  983.     contextClassLoader = cl;
  984.     }
  985.  
  986.     /* Some private helper methods */
  987.     private native void setPriority0(int newPriority);
  988.     private native void stop0(Object o);
  989.     private native void suspend0();
  990.     private native void resume0();
  991.     private native void interrupt0();
  992.  
  993. }
  994.