OSDN Git Service

2004-01-23 Michael Koch <konqueror@gmx.de>
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / Thread.java
1 /* Thread -- an independent thread of executable code
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation
4
5 This file is part of GNU Classpath.
6
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING.  If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA.
21
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library.  Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
26
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module.  An independent module is a module which is not derived from
34 or based on this library.  If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so.  If you do not wish to do so, delete this
37 exception statement from your version. */
38
39 package java.lang;
40
41 import gnu.gcj.RawData;
42
43 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
44  * "The Java Language Specification", ISBN 0-201-63451-1
45  * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
46  * Status:  Believed complete to version 1.4, with caveats. We do not 
47  *          implement the deprecated (and dangerous) stop, suspend, and resume
48  *          methods. Security implementation is not complete.
49  */
50
51 /**
52  * Thread represents a single thread of execution in the VM. When an
53  * application VM starts up, it creates a non-daemon Thread which calls the
54  * main() method of a particular class.  There may be other Threads running,
55  * such as the garbage collection thread.
56  *
57  * <p>Threads have names to identify them.  These names are not necessarily
58  * unique. Every Thread has a priority, as well, which tells the VM which
59  * Threads should get more running time. New threads inherit the priority
60  * and daemon status of the parent thread, by default.
61  *
62  * <p>There are two methods of creating a Thread: you may subclass Thread and
63  * implement the <code>run()</code> method, at which point you may start the
64  * Thread by calling its <code>start()</code> method, or you may implement
65  * <code>Runnable</code> in the class you want to use and then call new
66  * <code>Thread(your_obj).start()</code>.
67  *
68  * <p>The virtual machine runs until all non-daemon threads have died (either
69  * by returning from the run() method as invoked by start(), or by throwing
70  * an uncaught exception); or until <code>System.exit</code> is called with
71  * adequate permissions.
72  *
73  * <p>It is unclear at what point a Thread should be added to a ThreadGroup,
74  * and at what point it should be removed. Should it be inserted when it
75  * starts, or when it is created?  Should it be removed when it is suspended
76  * or interrupted?  The only thing that is clear is that the Thread should be
77  * removed when it is stopped.
78  *
79  * @author Tom Tromey
80  * @author John Keiser
81  * @author Eric Blake <ebb9@email.byu.edu>
82  * @see Runnable
83  * @see Runtime#exit(int)
84  * @see #run()
85  * @see #start()
86  * @see ThreadLocal
87  * @since 1.0
88  * @status updated to 1.4
89  */
90 public class Thread implements Runnable
91 {
92   /** The maximum priority for a Thread. */
93   public final static int MAX_PRIORITY = 10;
94
95   /** The minimum priority for a Thread. */
96   public final static int MIN_PRIORITY = 1;
97
98   /** The priority a Thread gets by default. */
99   public final static int NORM_PRIORITY = 5;
100
101   /**
102    * Get the number of active threads in the current Thread's ThreadGroup.
103    * This implementation calls
104    * <code>currentThread().getThreadGroup().activeCount()</code>.
105    *
106    * @return the number of active threads in the current ThreadGroup
107    * @see ThreadGroup#activeCount()
108    */
109   public static int activeCount ()
110   {
111     return currentThread().getThreadGroup().activeCount();
112   }
113
114   /**
115    * Check whether the current Thread is allowed to modify this Thread. This
116    * passes the check on to <code>SecurityManager.checkAccess(this)</code>.
117    *
118    * @throws SecurityException if the current Thread cannot modify this Thread
119    * @see SecurityManager#checkAccess(Thread)
120    */
121   public final void checkAccess ()
122   {
123     SecurityManager s = System.getSecurityManager();
124     if (s != null)
125       s.checkAccess(this);
126   }
127
128   /**
129    * Count the number of stack frames in this Thread.  The Thread in question
130    * must be suspended when this occurs.
131    *
132    * @return the number of stack frames in this Thread
133    * @throws IllegalThreadStateException if this Thread is not suspended
134    * @deprecated pointless, since suspend is deprecated
135    */
136   public native int countStackFrames ();
137
138   /**
139    * Get the currently executing Thread.
140    *
141    * @return the currently executing Thread
142    */
143   public static native Thread currentThread ();
144
145   /**
146    * Originally intended to destroy this thread, this method was never
147    * implemented by Sun, and is hence a no-op.
148    */
149   public native void destroy ();
150   
151   /**
152    * Print a stack trace of the current thread to stderr using the same
153    * format as Throwable's printStackTrace() method.
154    *
155    * @see Throwable#printStackTrace()
156    */
157   public static void dumpStack ()
158   {
159     (new Exception ("Stack trace")).printStackTrace ();
160   }
161
162   /**
163    * Copy every active thread in the current Thread's ThreadGroup into the
164    * array. Extra threads are silently ignored. This implementation calls
165    * <code>getThreadGroup().enumerate(array)</code>, which may have a
166    * security check, <code>checkAccess(group)</code>.
167    *
168    * @param array the array to place the Threads into
169    * @return the number of Threads placed into the array
170    * @throws NullPointerException if array is null
171    * @throws SecurityException if you cannot access the ThreadGroup
172    * @see ThreadGroup#enumerate(Thread[])
173    * @see #activeCount()
174    * @see SecurityManager#checkAccess(ThreadGroup)
175    */
176   public static int enumerate (Thread[] threads)
177   {
178     return currentThread().group.enumerate(threads);
179   }
180   
181   /**
182    * Get this Thread's name.
183    *
184    * @return this Thread's name
185    */
186   public final String getName ()
187   {
188     return name;
189   }
190
191   /**
192    * Get this Thread's priority.
193    *
194    * @return the Thread's priority
195    */
196   public final int getPriority ()
197   {
198     return priority;
199   }
200
201   /**
202    * Get the ThreadGroup this Thread belongs to. If the thread has died, this
203    * returns null.
204    *
205    * @return this Thread's ThreadGroup
206    */
207   public final ThreadGroup getThreadGroup ()
208   {
209     return group;
210   }
211
212   /**
213    * Return true if this Thread holds the object's lock, false otherwise.
214    *
215    * @param obj the object to test lock ownership on.
216    * @throws NullPointerException if obj is null.
217    * @since 1.4
218    */
219   public static native boolean holdsLock (Object obj);
220
221   /**
222    * Interrupt this Thread. First, there is a security check,
223    * <code>checkAccess</code>. Then, depending on the current state of the
224    * thread, various actions take place:
225    *
226    * <p>If the thread is waiting because of {@link #wait()},
227    * {@link #sleep(long)}, or {@link #join()}, its <i>interrupt status</i>
228    * will be cleared, and an InterruptedException will be thrown. Notice that
229    * this case is only possible if an external thread called interrupt().
230    *
231    * <p>If the thread is blocked in an interruptible I/O operation, in
232    * {@link java.nio.channels.InterruptibleChannel}, the <i>interrupt
233    * status</i> will be set, and ClosedByInterruptException will be thrown.
234    *
235    * <p>If the thread is blocked on a {@link java.nio.channels.Selector}, the
236    * <i>interrupt status</i> will be set, and the selection will return, with
237    * a possible non-zero value, as though by the wakeup() method.
238    *
239    * <p>Otherwise, the interrupt status will be set.
240    *
241    * @throws SecurityException if you cannot modify this Thread
242    */
243   public native void interrupt ();
244
245   /**
246    * Determine whether the current Thread has been interrupted, and clear
247    * the <i>interrupted status</i> in the process.
248    *
249    * @return whether the current Thread has been interrupted
250    * @see #isInterrupted()
251    */
252   public static boolean interrupted ()
253   {
254     return currentThread().isInterrupted (true);
255   }
256
257   /**
258    * Determine whether the given Thread has been interrupted, but leave
259    * the <i>interrupted status</i> alone in the process.
260    *
261    * @return whether the current Thread has been interrupted
262    * @see #interrupted()
263    */
264   public boolean isInterrupted ()
265   {
266     return interrupt_flag;
267   }
268
269   /**
270    * Determine whether this Thread is alive. A thread which is alive has
271    * started and not yet died.
272    *
273    * @return whether this Thread is alive
274    */
275   public final boolean isAlive ()
276   {
277     return alive_flag;
278   }
279
280   /**
281    * Tell whether this is a daemon Thread or not.
282    *
283    * @return whether this is a daemon Thread or not
284    * @see #setDaemon(boolean)
285    */
286   public final boolean isDaemon ()
287   {
288     return daemon_flag;
289   }
290
291   /**
292    * Wait forever for the Thread in question to die.
293    *
294    * @throws InterruptedException if the Thread is interrupted; it's
295    *         <i>interrupted status</i> will be cleared
296    */
297   public final void join () throws InterruptedException
298   {
299     join (0, 0);
300   }
301
302   /**
303    * Wait the specified amount of time for the Thread in question to die.
304    *
305    * @param ms the number of milliseconds to wait, or 0 for forever
306    * @throws InterruptedException if the Thread is interrupted; it's
307    *         <i>interrupted status</i> will be cleared
308    */
309   public final void join (long timeout) throws InterruptedException
310   {
311     join (timeout, 0);
312   }
313
314   /**
315    * Wait the specified amount of time for the Thread in question to die.
316    *
317    * <p>Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do
318    * not offer that fine a grain of timing resolution. Besides, there is
319    * no guarantee that this thread can start up immediately when time expires,
320    * because some other thread may be active.  So don't expect real-time
321    * performance.
322    *
323    * @param ms the number of milliseconds to wait, or 0 for forever
324    * @param ns the number of extra nanoseconds to sleep (0-999999)
325    * @throws InterruptedException if the Thread is interrupted; it's
326    *         <i>interrupted status</i> will be cleared
327    * @throws IllegalArgumentException if ns is invalid
328    * @XXX A ThreadListener would be nice, to make this efficient.
329    */
330   public final native void join (long timeout, int nanos)
331     throws InterruptedException;
332
333   /**
334    * Resume a suspended thread.
335    *
336    * @see #resume()
337    * @deprecated pointless, since suspend is deprecated
338    */
339   public final native void resume ();
340
341   private final native void finish_ ();
342
343   /**
344    * Determine whether the given Thread has been interrupted, but leave
345    * the <i>interrupted status</i> alone in the process.
346    *
347    * @return whether the current Thread has been interrupted
348    * @see #interrupted()
349    */
350   private boolean isInterrupted (boolean clear_flag)
351   {
352     boolean r = interrupt_flag;
353     if (clear_flag && r)
354       {
355         // Only clear the flag if we saw it as set. Otherwise this could 
356         // potentially cause us to miss an interrupt in a race condition, 
357         // because this method is not synchronized.
358         interrupt_flag = false;
359       }
360     return r;
361   }
362   
363   /**
364    * The method of Thread that will be run if there is no Runnable object
365    * associated with the Thread. Thread's implementation does nothing at all.
366    *
367    * @see #start()
368    * @see #Thread(ThreadGroup, Runnable, String)
369    */
370   public void run ()
371   {
372     if (runnable != null)
373       runnable.run();
374   }
375
376   /**
377    * Set the daemon status of this Thread.  If this is a daemon Thread, then
378    * the VM may exit even if it is still running.  This may only be called
379    * before the Thread starts running. There may be a security check,
380    * <code>checkAccess</code>.
381    *
382    * @param daemon whether this should be a daemon thread or not
383    * @throws SecurityException if you cannot modify this Thread
384    * @throws IllegalThreadStateException if the Thread is active
385    * @see #isDaemon()
386    * @see #checkAccess()
387    */
388   public final void setDaemon (boolean status)
389   {
390     checkAccess ();
391     if (!startable_flag)
392       throw new IllegalThreadStateException ();
393     daemon_flag = status;
394   }
395
396   /**
397    * Returns the context classloader of this Thread. The context
398    * classloader can be used by code that want to load classes depending
399    * on the current thread. Normally classes are loaded depending on
400    * the classloader of the current class. There may be a security check
401    * for <code>RuntimePermission("getClassLoader")</code> if the caller's
402    * class loader is not null or an ancestor of this thread's context class
403    * loader.
404    *
405    * @return the context class loader
406    * @throws SecurityException when permission is denied
407    * @see setContextClassLoader(ClassLoader)
408    * @since 1.2
409    */
410   public synchronized ClassLoader getContextClassLoader()
411   {
412     if (context_class_loader == null)
413       context_class_loader = ClassLoader.getSystemClassLoader ();
414
415     SecurityManager s = System.getSecurityManager();
416     // FIXME: we can't currently find the caller's class loader.
417     ClassLoader callers = null;
418     if (s != null && callers != null)
419       {
420         // See if the caller's class loader is the same as or an
421         // ancestor of this thread's class loader.
422         while (callers != null && callers != context_class_loader)
423           {
424             // FIXME: should use some internal version of getParent
425             // that avoids security checks.
426             callers = callers.getParent ();
427           }
428
429         if (callers != context_class_loader)
430           s.checkPermission (new RuntimePermission ("getClassLoader"));
431       }
432
433     return context_class_loader;
434   }
435
436   /**
437    * Returns the context classloader of this Thread. The context
438    * classloader can be used by code that want to load classes depending
439    * on the current thread. Normally classes are loaded depending on
440    * the classloader of the current class. There may be a security check
441    * for <code>RuntimePermission("getClassLoader")</code> if the caller's
442    * class loader is not null or an ancestor of this thread's context class
443    * loader.
444    *
445    * @return the context class loader
446    * @throws SecurityException when permission is denied
447    * @see setContextClassLoader(ClassLoader)
448    * @since 1.2
449    */
450   public synchronized void setContextClassLoader(ClassLoader cl)
451   {
452     SecurityManager s = System.getSecurityManager ();
453     if (s != null)
454       s.checkPermission (new RuntimePermission ("setContextClassLoader"));
455     context_class_loader = cl;
456   }
457
458   /**
459    * Set this Thread's name.  There may be a security check,
460    * <code>checkAccess</code>.
461    *
462    * @param name the new name for this Thread
463    * @throws NullPointerException if name is null
464    * @throws SecurityException if you cannot modify this Thread
465    */
466   public final void setName (String n)
467   {
468     checkAccess ();
469     // The Class Libraries book says ``threadName cannot be null''.  I
470     // take this to mean NullPointerException.
471     if (n == null)
472       throw new NullPointerException ();
473     name = n;
474   }
475
476   /**
477    * Set this Thread's priority. There may be a security check,
478    * <code>checkAccess</code>, then the priority is set to the smaller of
479    * priority and the ThreadGroup maximum priority.
480    *
481    * @param priority the new priority for this Thread
482    * @throws IllegalArgumentException if priority exceeds MIN_PRIORITY or
483    *         MAX_PRIORITY
484    * @throws SecurityException if you cannot modify this Thread
485    * @see #getPriority()
486    * @see #checkAccess()
487    * @see ThreadGroup#getMaxPriority()
488    * @see #MIN_PRIORITY
489    * @see #MAX_PRIORITY
490    */
491   public final native void setPriority (int newPriority);
492
493   /**
494    * Suspend the current Thread's execution for the specified amount of
495    * time. The Thread will not lose any locks it has during this time. There
496    * are no guarantees which thread will be next to run, but most VMs will
497    * choose the highest priority thread that has been waiting longest.
498    *
499    * @param ms the number of milliseconds to sleep, or 0 for forever
500    * @throws InterruptedException if the Thread is interrupted; it's
501    *         <i>interrupted status</i> will be cleared
502    * @see #notify()
503    * @see #wait(long)
504    */
505   public static void sleep (long timeout) throws InterruptedException
506   {
507     sleep (timeout, 0);
508   }
509
510   /**
511    * Suspend the current Thread's execution for the specified amount of
512    * time. The Thread will not lose any locks it has during this time. There
513    * are no guarantees which thread will be next to run, but most VMs will
514    * choose the highest priority thread that has been waiting longest.
515    *
516    * <p>Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do
517    * not offer that fine a grain of timing resolution. Besides, there is
518    * no guarantee that this thread can start up immediately when time expires,
519    * because some other thread may be active.  So don't expect real-time
520    * performance.
521    *
522    * @param ms the number of milliseconds to sleep, or 0 for forever
523    * @param ns the number of extra nanoseconds to sleep (0-999999)
524    * @throws InterruptedException if the Thread is interrupted; it's
525    *         <i>interrupted status</i> will be cleared
526    * @throws IllegalArgumentException if ns is invalid
527    * @see #notify()
528    * @see #wait(long, int)
529    */
530   public static native void sleep (long timeout, int nanos)
531     throws InterruptedException;
532
533   /**
534    * Start this Thread, calling the run() method of the Runnable this Thread
535    * was created with, or else the run() method of the Thread itself. This
536    * is the only way to start a new thread; calling run by yourself will just
537    * stay in the same thread. The virtual machine will remove the thread from
538    * its thread group when the run() method completes.
539    *
540    * @throws IllegalThreadStateException if the thread has already started
541    * @see #run()
542    */
543   public native void start ();
544
545   /**
546    * Cause this Thread to stop abnormally because of the throw of a ThreadDeath
547    * error. If you stop a Thread that has not yet started, it will stop
548    * immediately when it is actually started.
549    *
550    * <p>This is inherently unsafe, as it can interrupt synchronized blocks and
551    * leave data in bad states.  Hence, there is a security check:
552    * <code>checkAccess(this)</code>, plus another one if the current thread
553    * is not this: <code>RuntimePermission("stopThread")</code>. If you must
554    * catch a ThreadDeath, be sure to rethrow it after you have cleaned up.
555    * ThreadDeath is the only exception which does not print a stack trace when
556    * the thread dies.
557    *
558    * @throws SecurityException if you cannot stop the Thread
559    * @see #interrupt()
560    * @see #checkAccess()
561    * @see #start()
562    * @see ThreadDeath
563    * @see ThreadGroup#uncaughtException(Thread, Throwable)
564    * @see SecurityManager#checkAccess(Thread)
565    * @see SecurityManager#checkPermission(Permission)
566    * @deprecated unsafe operation, try not to use
567    */
568   public final void stop ()
569   {
570     // Argument doesn't matter, because this is no longer
571     // supported.
572     stop (null);
573   }
574
575   /**
576    * Cause this Thread to stop abnormally and throw the specified exception.
577    * If you stop a Thread that has not yet started, it will stop immediately
578    * when it is actually started. <b>WARNING</b>This bypasses Java security,
579    * and can throw a checked exception which the call stack is unprepared to
580    * handle. Do not abuse this power.
581    *
582    * <p>This is inherently unsafe, as it can interrupt synchronized blocks and
583    * leave data in bad states.  Hence, there is a security check:
584    * <code>checkAccess(this)</code>, plus another one if the current thread
585    * is not this: <code>RuntimePermission("stopThread")</code>. If you must
586    * catch a ThreadDeath, be sure to rethrow it after you have cleaned up.
587    * ThreadDeath is the only exception which does not print a stack trace when
588    * the thread dies.
589    *
590    * @param t the Throwable to throw when the Thread dies
591    * @throws SecurityException if you cannot stop the Thread
592    * @throws NullPointerException in the calling thread, if t is null
593    * @see #interrupt()
594    * @see #checkAccess()
595    * @see #start()
596    * @see ThreadDeath
597    * @see ThreadGroup#uncaughtException(Thread, Throwable)
598    * @see SecurityManager#checkAccess(Thread)
599    * @see SecurityManager#checkPermission(Permission)
600    * @deprecated unsafe operation, try not to use
601    */
602   public final native void stop (Throwable e);
603
604   /**
605    * Suspend this Thread.  It will not come back, ever, unless it is resumed.
606    *
607    * <p>This is inherently unsafe, as the suspended thread still holds locks,
608    * and can potentially deadlock your program.  Hence, there is a security
609    * check: <code>checkAccess</code>.
610    *
611    * @throws SecurityException if you cannot suspend the Thread
612    * @see #checkAccess()
613    * @see #resume()
614    * @deprecated unsafe operation, try not to use
615    */
616   public final native void suspend ();
617
618   private final native void initialize_native ();
619
620   private final native static String gen_name ();
621
622   /**
623    * Allocate a new Thread object, with the specified ThreadGroup and name, and
624    * using the specified Runnable object's <code>run()</code> method to
625    * execute.  If the Runnable object is null, <code>this</code> (which is
626    * a Runnable) is used instead.
627    *
628    * <p>If the ThreadGroup is null, the security manager is checked. If a
629    * manager exists and returns a non-null object for
630    * <code>getThreadGroup</code>, that group is used; otherwise the group
631    * of the creating thread is used. Note that the security manager calls
632    * <code>checkAccess</code> if the ThreadGroup is not null.
633    *
634    * <p>The new Thread will inherit its creator's priority and daemon status.
635    * These can be changed with <code>setPriority</code> and
636    * <code>setDaemon</code>.
637    *
638    * @param group the group to put the Thread into
639    * @param target the Runnable object to execute
640    * @param name the name for the Thread
641    * @throws NullPointerException if name is null
642    * @throws SecurityException if this thread cannot access <code>group</code>
643    * @throws IllegalThreadStateException if group is destroyed
644    * @see Runnable#run()
645    * @see #run()
646    * @see #setDaemon(boolean)
647    * @see #setPriority(int)
648    * @see SecurityManager#checkAccess(ThreadGroup)
649    * @see ThreadGroup#checkAccess()
650    */
651   public Thread (ThreadGroup g, Runnable r, String n)
652   {
653     this (currentThread (), g, r, n);
654   }
655
656   /**
657    * Allocate a new Thread object, as if by
658    * <code>Thread(group, null, name)</code>, and give it the specified stack
659    * size, in bytes. The stack size is <b>highly platform independent</b>,
660    * and the virtual machine is free to round up or down, or ignore it
661    * completely.  A higher value might let you go longer before a
662    * <code>StackOverflowError</code>, while a lower value might let you go
663    * longer before an <code>OutOfMemoryError</code>.  Or, it may do absolutely
664    * nothing! So be careful, and expect to need to tune this value if your
665    * virtual machine even supports it.
666    *
667    * @param group the group to put the Thread into
668    * @param target the Runnable object to execute
669    * @param name the name for the Thread
670    * @param size the stack size, in bytes; 0 to be ignored
671    * @throws NullPointerException if name is null
672    * @throws SecurityException if this thread cannot access <code>group</code>
673    * @throws IllegalThreadStateException if group is destroyed
674    * @since 1.4
675    */
676   public Thread (ThreadGroup g, Runnable r, String n, long size)
677   {
678     // Just ignore stackSize for now.
679     this (currentThread (), g, r, n);
680   }
681
682   private Thread (Thread current, ThreadGroup g, Runnable r, String n)
683   {
684     // The Class Libraries book says ``threadName cannot be null''.  I
685     // take this to mean NullPointerException.
686     if (n == null)
687       throw new NullPointerException ();
688       
689     if (g == null)
690       {
691         // If CURRENT is null, then we are bootstrapping the first thread. 
692         // Use ThreadGroup.root, the main threadgroup.
693         if (current == null)
694           group = ThreadGroup.root;
695         else
696           group = current.getThreadGroup();
697       }
698     else
699       group = g;
700       
701     data = null;
702     interrupt_flag = false;
703     alive_flag = false;
704     startable_flag = true;
705
706     if (current != null)
707       {
708         group.checkAccess();
709
710         daemon_flag = current.isDaemon();
711         int gmax = group.getMaxPriority();
712         int pri = current.getPriority();
713         priority = (gmax < pri ? gmax : pri);
714         context_class_loader = current.context_class_loader;
715         InheritableThreadLocal.newChildThread(this);
716       }
717     else
718       {
719         daemon_flag = false;
720         priority = NORM_PRIORITY;
721       }
722
723     name = n;
724     group.addThread(this);
725     runnable = r;
726
727     initialize_native ();
728   }
729
730   /**
731    * Allocates a new <code>Thread</code> object. This constructor has
732    * the same effect as <code>Thread(null, null,</code>
733    * <i>gname</i><code>)</code>, where <b><i>gname</i></b> is
734    * a newly generated name. Automatically generated names are of the
735    * form <code>"Thread-"+</code><i>n</i>, where <i>n</i> is an integer.
736    * <p>
737    * Threads created this way must have overridden their
738    * <code>run()</code> method to actually do anything.  An example
739    * illustrating this method being used follows:
740    * <p><blockquote><pre>
741    *     import java.lang.*;
742    *
743    *     class plain01 implements Runnable {
744    *         String name;
745    *         plain01() {
746    *             name = null;
747    *         }
748    *         plain01(String s) {
749    *             name = s;
750    *         }
751    *         public void run() {
752    *             if (name == null)
753    *                 System.out.println("A new thread created");
754    *             else
755    *                 System.out.println("A new thread with name " + name +
756    *                                    " created");
757    *         }
758    *     }
759    *     class threadtest01 {
760    *         public static void main(String args[] ) {
761    *             int failed = 0 ;
762    *
763    *             <b>Thread t1 = new Thread();</b>
764    *             if (t1 != null)
765    *                 System.out.println("new Thread() succeed");
766    *             else {
767    *                 System.out.println("new Thread() failed");
768    *                 failed++;
769    *             }
770    *         }
771    *     }
772    * </pre></blockquote>
773    *
774    * @see     java.lang.Thread#Thread(java.lang.ThreadGroup,
775    *          java.lang.Runnable, java.lang.String)
776    */
777   public Thread ()
778   {
779     this (null, null, gen_name ());
780   }
781
782   /**
783    * Allocates a new <code>Thread</code> object. This constructor has
784    * the same effect as <code>Thread(null, target,</code>
785    * <i>gname</i><code>)</code>, where <i>gname</i> is
786    * a newly generated name. Automatically generated names are of the
787    * form <code>"Thread-"+</code><i>n</i>, where <i>n</i> is an integer.
788    *
789    * @param   target   the object whose <code>run</code> method is called.
790    * @see     java.lang.Thread#Thread(java.lang.ThreadGroup,
791    *          java.lang.Runnable, java.lang.String)
792    */
793   public Thread (Runnable r)
794   {
795     this (null, r, gen_name ());
796   }
797
798   /**
799    * Allocates a new <code>Thread</code> object. This constructor has
800    * the same effect as <code>Thread(null, null, name)</code>.
801    *
802    * @param   name   the name of the new thread.
803    * @see     java.lang.Thread#Thread(java.lang.ThreadGroup,
804    *          java.lang.Runnable, java.lang.String)
805    */
806   public Thread (String n)
807   {
808     this (null, null, n);
809   }
810
811   /**
812    * Allocates a new <code>Thread</code> object. This constructor has
813    * the same effect as <code>Thread(group, target,</code>
814    * <i>gname</i><code>)</code>, where <i>gname</i> is
815    * a newly generated name. Automatically generated names are of the
816    * form <code>"Thread-"+</code><i>n</i>, where <i>n</i> is an integer.
817    *
818    * @param      group    the thread group.
819    * @param      target   the object whose <code>run</code> method is called.
820    * @exception  SecurityException  if the current thread cannot create a
821    *             thread in the specified thread group.
822    * @see        java.lang.Thread#Thread(java.lang.ThreadGroup,
823    *             java.lang.Runnable, java.lang.String)
824    */
825   public Thread (ThreadGroup g, Runnable r)
826   {
827     this (g, r, gen_name ());
828   }
829
830   /**
831    * Allocates a new <code>Thread</code> object. This constructor has
832    * the same effect as <code>Thread(group, null, name)</code>
833    *
834    * @param      group   the thread group.
835    * @param      name    the name of the new thread.
836    * @exception  SecurityException  if the current thread cannot create a
837    *               thread in the specified thread group.
838    * @see        java.lang.Thread#Thread(java.lang.ThreadGroup,
839    *          java.lang.Runnable, java.lang.String)
840    */
841   public Thread (ThreadGroup g, String n)
842   {
843     this (g, null, n);
844   }
845
846   /**
847    * Allocates a new <code>Thread</code> object. This constructor has
848    * the same effect as <code>Thread(null, target, name)</code>.
849    *
850    * @param   target   the object whose <code>run</code> method is called.
851    * @param   name     the name of the new thread.
852    * @see     java.lang.Thread#Thread(java.lang.ThreadGroup,
853    *          java.lang.Runnable, java.lang.String)
854    */
855   public Thread (Runnable r, String n)
856   {
857     this (null, r, n);
858   }
859
860   /**
861    * Returns a string representation of this thread, including the
862    * thread's name, priority, and thread group.
863    *
864    * @return  a string representation of this thread.
865    */
866   public String toString ()
867   {
868     return "Thread[" + name + "," + priority + "," + 
869       (group == null ? "" : group.getName()) + "]";
870   }
871
872   /**
873    * Causes the currently executing thread object to temporarily pause
874    * and allow other threads to execute.
875    */
876   public static native void yield ();
877
878   // Private data.
879   ThreadGroup group;
880   String name;
881   private Runnable runnable;
882   private int priority;
883   private boolean daemon_flag;
884   boolean interrupt_flag;
885   private boolean alive_flag;
886   private boolean startable_flag;
887   private ClassLoader context_class_loader;
888
889   // This describes the top-most interpreter frame for this thread.
890   RawData interp_frame;
891
892   // Our native data - points to an instance of struct natThread.
893   private Object data;
894 }