]> gcc.gnu.org Git - gcc.git/blob - libjava/java/lang/Thread.java
30fb4cd52a8a371e4bdb8a742b20f22183406466
[gcc.git] / libjava / java / lang / Thread.java
1 // Thread.java - Thread class.
2
3 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 package java.lang;
12
13 /**
14 * @author Tom Tromey <tromey@cygnus.com>
15 * @date August 24, 1998
16 */
17
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.
24 */
25
26 public class Thread implements Runnable
27 {
28 public final static int MAX_PRIORITY = 10;
29 public final static int MIN_PRIORITY = 1;
30 public final static int NORM_PRIORITY = 5;
31
32 public static int activeCount ()
33 {
34 return currentThread().getThreadGroup().activeCount();
35 }
36
37 public final void checkAccess ()
38 {
39 SecurityManager s = System.getSecurityManager();
40 if (s != null)
41 s.checkAccess(this);
42 }
43
44 public native int countStackFrames ();
45 public static native Thread currentThread ();
46 public native void destroy ();
47
48 public static void dumpStack ()
49 {
50 (new Exception ("Stack trace")).printStackTrace ();
51 }
52
53 public static int enumerate (Thread[] threads)
54 {
55 return currentThread().group.enumerate(threads);
56 }
57
58 public final String getName ()
59 {
60 return name;
61 }
62
63 public final int getPriority ()
64 {
65 return priority;
66 }
67
68 public final ThreadGroup getThreadGroup ()
69 {
70 return group;
71 }
72
73 public native void interrupt ();
74
75 public static boolean interrupted ()
76 {
77 return currentThread().isInterrupted (true);
78 }
79
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 ()
83 {
84 return interrupt_flag;
85 }
86
87 public final boolean isAlive ()
88 {
89 return alive_flag;
90 }
91
92 public final boolean isDaemon ()
93 {
94 return daemon_flag;
95 }
96
97 public final void join () throws InterruptedException
98 {
99 join (0, 0);
100 }
101
102 public final void join (long timeout) throws InterruptedException
103 {
104 join (timeout, 0);
105 }
106
107 public final native void join (long timeout, int nanos)
108 throws InterruptedException;
109
110 public final native void resume ();
111
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_ ();
115
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)
119 {
120 boolean r = interrupt_flag;
121 if (clear_flag && r)
122 {
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;
127 }
128 return r;
129 }
130
131 public void run ()
132 {
133 if (runnable != null)
134 runnable.run();
135 }
136
137 public final void setDaemon (boolean status)
138 {
139 checkAccess ();
140 if (isAlive ())
141 throw new IllegalThreadStateException ();
142 daemon_flag = status;
143 }
144
145 public synchronized ClassLoader getContextClassLoader()
146 {
147 if (context_class_loader == null)
148 context_class_loader = ClassLoader.getSystemClassLoader ();
149
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)
154 {
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)
158 {
159 // FIXME: should use some internal version of getParent
160 // that avoids security checks.
161 callers = callers.getParent ();
162 }
163
164 if (callers != context_class_loader)
165 s.checkPermission (new RuntimePermission ("getClassLoader"));
166 }
167
168 return context_class_loader;
169 }
170
171 public synchronized void setContextClassLoader(ClassLoader cl)
172 {
173 SecurityManager s = System.getSecurityManager ();
174 if (s != null)
175 s.checkPermission (new RuntimePermission ("setContextClassLoader"));
176 context_class_loader = cl;
177 }
178
179 public final void setName (String n)
180 {
181 checkAccess ();
182 // The Class Libraries book says ``threadName cannot be null''. I
183 // take this to mean NullPointerException.
184 if (n == null)
185 throw new NullPointerException ();
186 name = n;
187 }
188
189 public final native void setPriority (int newPriority);
190
191 public static void sleep (long timeout) throws InterruptedException
192 {
193 sleep (timeout, 0);
194 }
195
196 public static native void sleep (long timeout, int nanos)
197 throws InterruptedException;
198 public synchronized native void start ();
199
200 public final void stop ()
201 {
202 // Argument doesn't matter, because this is no longer
203 // supported.
204 stop (null);
205 }
206
207 public final synchronized native void stop (Throwable e);
208 public final native void suspend ();
209
210 private final native void initialize_native ();
211
212 private final synchronized static String gen_name ()
213 {
214 String n;
215 n = "Thread-" + nextThreadNumber;
216 ++nextThreadNumber;
217 return n;
218 }
219
220 public Thread (ThreadGroup g, Runnable r, String n)
221 {
222 Thread current = currentThread ();
223
224 if (g == null)
225 {
226 // If CURRENT is null, then we are bootstrapping the first thread.
227 // Use ThreadGroup.root, the main threadgroup.
228 if (current == null)
229 group = ThreadGroup.root;
230 else
231 group = current.getThreadGroup();
232 }
233 else
234 group = g;
235
236 group.checkAccess();
237
238 // The Class Libraries book says ``threadName cannot be null''. I
239 // take this to mean NullPointerException.
240 if (n == null)
241 throw new NullPointerException ();
242
243 name = n;
244 group.addThread(this);
245 runnable = r;
246
247 data = null;
248 interrupt_flag = false;
249 alive_flag = false;
250 startable_flag = true;
251
252 if (current != null)
253 {
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;
259 }
260 else
261 {
262 daemon_flag = false;
263 priority = NORM_PRIORITY;
264 }
265
266 initialize_native ();
267 }
268
269 public Thread ()
270 {
271 this (null, null, gen_name ());
272 }
273
274 public Thread (Runnable r)
275 {
276 this (null, r, gen_name ());
277 }
278
279 public Thread (String n)
280 {
281 this (null, null, n);
282 }
283
284 public Thread (ThreadGroup g, Runnable r)
285 {
286 this (g, r, gen_name ());
287 }
288
289 public Thread (ThreadGroup g, String n)
290 {
291 this (g, null, n);
292 }
293
294 public Thread (Runnable r, String n)
295 {
296 this (null, r, n);
297 }
298
299 public String toString ()
300 {
301 return "Thread[" + name + "," + priority + "," +
302 (group == null ? "" : group.getName()) + "]";
303 }
304
305 public static native void yield ();
306
307 // Private data.
308 private ThreadGroup group;
309 private String name;
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;
317
318 // Our native data.
319 private Object data;
320
321 // Next thread number to assign.
322 private static int nextThreadNumber = 0;
323 }
This page took 0.055174 seconds and 4 git commands to generate.