1 // Thread.java - Thread class.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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 private final native void finish_ ();
114 // Check the thread's interrupted status. If clear_flag is true, the
115 // thread's interrupted status is also cleared.
116 private boolean isInterrupted (boolean clear_flag)
118 boolean r = interrupt_flag;
121 // Only clear the flag if we saw it as set. Otherwise this could
122 // potentially cause us to miss an interrupt in a race condition,
123 // because this method is not synchronized.
124 interrupt_flag = false;
131 if (runnable != null)
135 public final void setDaemon (boolean status)
139 throw new IllegalThreadStateException ();
140 daemon_flag = status;
143 public synchronized ClassLoader getContextClassLoader()
145 if (context_class_loader == null)
146 context_class_loader = ClassLoader.getSystemClassLoader ();
148 SecurityManager s = System.getSecurityManager();
149 // FIXME: we can't currently find the caller's class loader.
150 ClassLoader callers = null;
151 if (s != null && callers != null)
153 // See if the caller's class loader is the same as or an
154 // ancestor of this thread's class loader.
155 while (callers != null && callers != context_class_loader)
157 // FIXME: should use some internal version of getParent
158 // that avoids security checks.
159 callers = callers.getParent ();
162 if (callers != context_class_loader)
163 s.checkPermission (new RuntimePermission ("getClassLoader"));
166 return context_class_loader;
169 public synchronized void setContextClassLoader(ClassLoader cl)
171 SecurityManager s = System.getSecurityManager ();
173 s.checkPermission (new RuntimePermission ("setContextClassLoader"));
174 context_class_loader = cl;
177 public final void setName (String n)
180 // The Class Libraries book says ``threadName cannot be null''. I
181 // take this to mean NullPointerException.
183 throw new NullPointerException ();
187 public final native void setPriority (int newPriority);
189 public static void sleep (long timeout) throws InterruptedException
194 public static native void sleep (long timeout, int nanos)
195 throws InterruptedException;
196 public native void start ();
198 public final void stop ()
200 // Argument doesn't matter, because this is no longer
205 public final native void stop (Throwable e);
206 public final native void suspend ();
208 private final native void initialize_native ();
210 private final native static String gen_name ();
212 public Thread (ThreadGroup g, Runnable r, String n)
214 this (currentThread (), g, r, n);
216 // The Class Libraries book says ``threadName cannot be null''. I
217 // take this to mean NullPointerException.
219 throw new NullPointerException ();
222 private Thread (Thread current, ThreadGroup g, Runnable r, String n)
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();
237 interrupt_flag = false;
239 startable_flag = true;
245 daemon_flag = current.isDaemon();
246 int gmax = group.getMaxPriority();
247 int pri = current.getPriority();
248 priority = (gmax < pri ? gmax : pri);
249 context_class_loader = current.context_class_loader;
250 InheritableThreadLocal.newChildThread(this);
255 priority = NORM_PRIORITY;
259 group.addThread(this);
262 initialize_native ();
267 this (null, null, gen_name ());
270 public Thread (Runnable r)
272 this (null, r, gen_name ());
275 public Thread (String n)
277 this (null, null, n);
280 public Thread (ThreadGroup g, Runnable r)
282 this (g, r, gen_name ());
285 public Thread (ThreadGroup g, String n)
290 public Thread (Runnable r, String n)
295 public String toString ()
297 return "Thread[" + name + "," + priority + "," +
298 (group == null ? "" : group.getName()) + "]";
301 public static native void yield ();
304 private ThreadGroup group;
306 private Runnable runnable;
307 private int priority;
308 private boolean daemon_flag;
309 boolean interrupt_flag;
310 private boolean alive_flag;
311 private boolean startable_flag;
312 private ClassLoader context_class_loader;
314 // Our native data - points to an instance of struct natThread.