1 // Thread.java - Thread class.
3 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
14 * @author Tom Tromey <tromey@cygnus.com>
15 * @date August 24, 1998
18 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
19 * "The Java Language Specification", ISBN 0-201-63451-1
20 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
21 * Status: Believed complete to version 1.3, with caveats. We do not
22 * implement the deprecated (and dangerous) stop, suspend, and resume
23 * methods. Security implementation is not complete.
26 public class Thread
implements Runnable
28 public final static int MAX_PRIORITY
= 10;
29 public final static int MIN_PRIORITY
= 1;
30 public final static int NORM_PRIORITY
= 5;
32 public static int activeCount ()
34 return currentThread().getThreadGroup().activeCount();
37 public final void checkAccess ()
39 SecurityManager s
= System
.getSecurityManager();
44 public native int countStackFrames ();
45 public static native Thread
currentThread ();
46 public native void destroy ();
48 public static void dumpStack ()
50 (new Exception ("Stack trace")).printStackTrace ();
53 public static int enumerate (Thread
[] threads
)
55 return currentThread().group
.enumerate(threads
);
58 public final String
getName ()
63 public final int getPriority ()
68 public final ThreadGroup
getThreadGroup ()
73 public native void interrupt ();
75 public static boolean interrupted ()
77 return currentThread().isInterrupted (true);
80 // Check the threads interrupted status. Note that this does not clear the
81 // thread's interrupted status (per JDK 1.2 online API documentation).
82 public boolean isInterrupted ()
84 return interrupt_flag
;
87 public final boolean isAlive ()
92 public final boolean isDaemon ()
97 public final void join () throws InterruptedException
102 public final void join (long timeout
) throws InterruptedException
107 public final native void join (long timeout
, int nanos
)
108 throws InterruptedException
;
110 public final native void resume ();
112 // This method exists only to avoid a warning from the C++ compiler.
113 private static final native void run_ (Object obj
);
114 private final native void finish_ ();
116 // Check the thread's interrupted status. If clear_flag is true, the
117 // thread's interrupted status is also cleared.
118 private boolean isInterrupted (boolean clear_flag
)
120 boolean r
= interrupt_flag
;
123 // Only clear the flag if we saw it as set. Otherwise this could
124 // potentially cause us to miss an interrupt in a race condition,
125 // because this method is not synchronized.
126 interrupt_flag
= false;
133 if (runnable
!= null)
137 public final void setDaemon (boolean status
)
141 throw new IllegalThreadStateException ();
142 daemon_flag
= status
;
145 public synchronized ClassLoader
getContextClassLoader()
147 if (context_class_loader
== null)
148 context_class_loader
= ClassLoader
.getSystemClassLoader ();
150 SecurityManager s
= System
.getSecurityManager();
151 // FIXME: we can't currently find the caller's class loader.
152 ClassLoader callers
= null;
153 if (s
!= null && callers
!= null)
155 // See if the caller's class loader is the same as or an
156 // ancestor of this thread's class loader.
157 while (callers
!= null && callers
!= context_class_loader
)
159 // FIXME: should use some internal version of getParent
160 // that avoids security checks.
161 callers
= callers
.getParent ();
164 if (callers
!= context_class_loader
)
165 s
.checkPermission (new RuntimePermission ("getClassLoader"));
168 return context_class_loader
;
171 public synchronized void setContextClassLoader(ClassLoader cl
)
173 SecurityManager s
= System
.getSecurityManager ();
175 s
.checkPermission (new RuntimePermission ("setContextClassLoader"));
176 context_class_loader
= cl
;
179 public final void setName (String n
)
182 // The Class Libraries book says ``threadName cannot be null''. I
183 // take this to mean NullPointerException.
185 throw new NullPointerException ();
189 public final native void setPriority (int newPriority
);
191 public static void sleep (long timeout
) throws InterruptedException
196 public static native void sleep (long timeout
, int nanos
)
197 throws InterruptedException
;
198 public synchronized native void start ();
200 public final void stop ()
202 // Argument doesn't matter, because this is no longer
207 public final synchronized native void stop (Throwable e
);
208 public final native void suspend ();
210 private final native void initialize_native ();
212 private final synchronized static String
gen_name ()
215 n
= "Thread-" + nextThreadNumber
;
220 public Thread (ThreadGroup g
, Runnable r
, String n
)
222 Thread current
= currentThread ();
226 // If CURRENT is null, then we are bootstrapping the first thread.
227 // Use ThreadGroup.root, the main threadgroup.
229 group
= ThreadGroup
.root
;
231 group
= current
.getThreadGroup();
238 // The Class Libraries book says ``threadName cannot be null''. I
239 // take this to mean NullPointerException.
241 throw new NullPointerException ();
244 group
.addThread(this);
248 interrupt_flag
= false;
250 startable_flag
= true;
254 daemon_flag
= current
.isDaemon();
255 int gmax
= group
.getMaxPriority();
256 int pri
= current
.getPriority();
257 priority
= (gmax
< pri ? gmax
: pri
);
258 context_class_loader
= current
.context_class_loader
;
263 priority
= NORM_PRIORITY
;
266 initialize_native ();
271 this (null, null, gen_name ());
274 public Thread (Runnable r
)
276 this (null, r
, gen_name ());
279 public Thread (String n
)
281 this (null, null, n
);
284 public Thread (ThreadGroup g
, Runnable r
)
286 this (g
, r
, gen_name ());
289 public Thread (ThreadGroup g
, String n
)
294 public Thread (Runnable r
, String n
)
299 public String
toString ()
301 return "Thread[" + name
+ "," + priority
+ "," +
302 (group
== null ?
"" : group
.getName()) + "]";
305 public static native void yield ();
308 private ThreadGroup group
;
310 private Runnable runnable
;
311 private int priority
;
312 private boolean daemon_flag
;
313 boolean interrupt_flag
;
314 private boolean alive_flag
;
315 private boolean startable_flag
;
316 private ClassLoader context_class_loader
;
321 // Next thread number to assign.
322 private static int nextThreadNumber
= 0;