1 // Thread.java - Thread class.
3 /* Copyright (C) 1998, 1999, 2000 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
13 import gnu.gcj.RawData;
16 * @author Tom Tromey <tromey@cygnus.com>
17 * @date August 24, 1998
20 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
21 * "The Java Language Specification", ISBN 0-201-63451-1
22 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
23 * Status: Complete to version 1.1, with caveats
25 * No attempt was made to implement suspend/resume
26 * (this could be done in some cases)
27 * Various methods which assume a VM are likewise unimplemented
28 * We do implement stop() even though it is deprecated.
31 public class Thread implements Runnable
33 public final static int MAX_PRIORITY = 10;
34 public final static int MIN_PRIORITY = 1;
35 public final static int NORM_PRIORITY = 5;
37 public static int activeCount ()
39 return currentThread().getThreadGroup().activeCount();
42 public void checkAccess ()
44 SecurityManager s = System.getSecurityManager();
49 public native int countStackFrames ();
50 public static native Thread currentThread ();
51 public native void destroy ();
53 public static void dumpStack ()
55 (new Exception ("Stack trace")).printStackTrace ();
58 public static int enumerate (Thread[] threads)
60 return currentThread().group.enumerate(threads);
63 public final String getName ()
68 public final int getPriority ()
73 public final ThreadGroup getThreadGroup ()
78 public native void interrupt ();
80 public static boolean interrupted ()
82 return currentThread().isInterrupted_();
85 // FIXME: it seems to me that this should be synchronized.
86 // Check the threads interrupted status. Note that this does not clear the
87 // threads interrupted status (per JDK 1.2 online API documentation).
88 public boolean isInterrupted ()
90 return interrupt_flag;
93 public final boolean isAlive ()
98 public final boolean isDaemon ()
103 public final void join () throws InterruptedException
108 public final void join (long timeout) throws InterruptedException
113 public final native void join (long timeout, int nanos)
114 throws InterruptedException;
116 public final native void resume ();
118 // This method exists only to avoid a warning from the C++ compiler.
119 private static final native void run_ (Object obj);
120 private final native void finish_ ();
122 // Convenience method to check and clear the thread's interrupted status.
123 private boolean isInterrupted_ ()
125 boolean r = interrupt_flag;
126 interrupt_flag = false;
132 if (runnable != null)
136 public final void setDaemon (boolean status)
140 throw new IllegalThreadStateException ();
141 daemon_flag = status;
145 // public ClassLoader getContextClassLoader()
150 // public void setContextClassLoader(ClassLoader cl)
154 public final void setName (String n)
157 // The Class Libraries book says ``threadName cannot be null''. I
158 // take this to mean NullPointerException.
160 throw new NullPointerException ();
164 public final native void setPriority (int newPriority);
166 public static void sleep (long timeout) throws InterruptedException
171 public static native void sleep (long timeout, int nanos)
172 throws InterruptedException;
173 public synchronized native void start ();
175 public final void stop ()
177 // Argument doesn't matter, because this is no longer
182 public final synchronized native void stop (Throwable e);
183 public final native void suspend ();
185 private final native void initialize_native ();
187 private final synchronized static String gen_name ()
190 n = "Thread-" + nextThreadNumber;
195 public Thread (ThreadGroup g, Runnable r, String n)
197 // Note that CURRENT can be null when we are creating the very
198 // first thread. That's why we check it below.
199 Thread current = currentThread ();
203 // If CURRENT is null, then we are creating the first thread.
204 // In this case we don't do the security check.
209 g = current.getThreadGroup();
211 // The Class Libraries book says ``threadName cannot be null''. I
212 // take this to mean NullPointerException.
214 throw new NullPointerException ();
222 interrupt_flag = false;
226 daemon_flag = current.isDaemon();
227 priority = current.getPriority();
232 priority = NORM_PRIORITY;
235 initialize_native ();
240 this (null, null, gen_name ());
243 public Thread (Runnable r)
245 this (null, r, gen_name ());
248 public Thread (String n)
250 this (null, null, n);
253 public Thread (ThreadGroup g, Runnable r)
255 this (g, r, gen_name ());
258 public Thread (ThreadGroup g, String n)
263 public Thread (Runnable r, String n)
268 public String toString ()
270 return "Thread[" + name + "," + priority + "," + group.getName() + "]";
273 public static native void yield ();
276 private ThreadGroup group;
278 private Runnable runnable;
279 private int priority;
280 private boolean daemon_flag;
281 private boolean interrupt_flag;
282 private boolean alive_flag;
285 private RawData data;
287 // Next thread number to assign.
288 private static int nextThreadNumber = 0;