home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Thread.java < prev    next >
Text File  |  1997-05-20  |  35KB  |  964 lines

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