1 // Thread.java - Thread class.
3 /* Copyright (C) 1998, 1999 Red Hat, Inc.
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: Complete to version 1.1, with caveats
23 * No attempt was made to implement suspend/resume
24 * (this could be done in some cases)
25 * Various methods which assume a VM are likewise unimplemented
26 * We do implement stop() even though it is deprecated.
29 public class Thread implements Runnable
31 public final static int MAX_PRIORITY = 10;
32 public final static int MIN_PRIORITY = 1;
33 public final static int NORM_PRIORITY = 5;
35 public static int activeCount ()
37 return currentThread().getThreadGroup().activeCount();
40 public void checkAccess ()
42 SecurityManager s = System.getSecurityManager();
47 public native int countStackFrames ();
48 public static native Thread currentThread ();
49 public native void destroy ();
50 public static native void dumpStack ();
52 public static int enumerate (Thread[] threads)
54 return currentThread().group.enumerate(threads);
57 public final String getName ()
62 public final int getPriority ()
67 public final ThreadGroup getThreadGroup ()
72 public native void interrupt ();
74 public static boolean interrupted ()
76 return currentThread().isInterrupted_();
79 // FIXME: it seems to me that this should be synchronized.
80 // Check the threads interrupted status. Note that this does not clear the
81 // threads 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 native final void finish_ ();
116 // Convenience method to check and clear the thread's interrupted status.
117 private boolean isInterrupted_ ()
119 boolean r = interrupt_flag;
120 interrupt_flag = false;
124 private final void run_ ()
132 // Uncaught exceptions are forwarded to the ThreadGroup. If
133 // this results in an uncaught exception, that is ignored.
136 group.uncaughtException(this, e);
148 if (runnable != null)
152 public final void setDaemon (boolean status)
156 throw new IllegalThreadStateException ();
157 daemon_flag = status;
161 // public ClassLoader getContextClassLoader()
166 // public void setContextClassLoader(ClassLoader cl)
170 public final void setName (String n)
173 // The Class Libraries book says ``threadName cannot be null''. I
174 // take this to mean NullPointerException.
176 throw new NullPointerException ();
180 public final native void setPriority (int newPriority);
182 public static void sleep (long timeout) throws InterruptedException
187 public static native void sleep (long timeout, int nanos)
188 throws InterruptedException;
189 public synchronized native void start ();
191 public final void stop ()
193 // Argument doesn't matter, because this is no longer
198 public final synchronized native void stop (Throwable e);
199 public final native void suspend ();
201 private final native void initialize_native ();
203 private final synchronized static String gen_name ()
206 n = "Thread-" + nextThreadNumber;
211 public Thread (ThreadGroup g, Runnable r, String n)
213 // Note that CURRENT can be null when we are creating the very
214 // first thread. That's why we check it below.
215 Thread current = currentThread ();
219 // If CURRENT is null, then we are creating the first thread.
220 // In this case we don't do the security check.
225 g = current.getThreadGroup();
227 // The Class Libraries book says ``threadName cannot be null''. I
228 // take this to mean NullPointerException.
230 throw new NullPointerException ();
238 interrupt_flag = false;
242 daemon_flag = current.isDaemon();
243 priority = current.getPriority();
248 priority = NORM_PRIORITY;
251 initialize_native ();
256 this (null, null, gen_name ());
259 public Thread (Runnable r)
261 this (null, r, gen_name ());
264 public Thread (String n)
266 this (null, null, n);
269 public Thread (ThreadGroup g, Runnable r)
271 this (g, r, gen_name ());
274 public Thread (ThreadGroup g, String n)
279 public Thread (Runnable r, String n)
284 public String toString ()
286 return "Thread[" + name + "," + priority + "," + group.getName() + "]";
289 public static native void yield ();
292 private ThreadGroup group;
294 private Runnable runnable;
295 private int priority;
296 private boolean daemon_flag;
297 private boolean interrupt_flag;
298 private boolean alive_flag;
300 // This is a bit odd. We need a way to represent some data that is
301 // manipulated only by the native side of this class. We represent
302 // it as a Java object reference. However, it is not actually a
306 // Next thread number to assign.
307 private static int nextThreadNumber = 0;