1 | // Thread.java - Thread class.
|
---|
2 |
|
---|
3 | /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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 | import gnu.gcj.RawData;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * @author Tom Tromey <tromey@cygnus.com>
|
---|
17 | * @date August 24, 1998
|
---|
18 | */
|
---|
19 |
|
---|
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: Believed complete to version 1.3, with caveats. We do not
|
---|
24 | * implement the deprecated (and dangerous) stop, suspend, and resume
|
---|
25 | * methods. Security implementation is not complete.
|
---|
26 | */
|
---|
27 |
|
---|
28 | public class Thread implements Runnable
|
---|
29 | {
|
---|
30 | public final static int MAX_PRIORITY = 10;
|
---|
31 | public final static int MIN_PRIORITY = 1;
|
---|
32 | public final static int NORM_PRIORITY = 5;
|
---|
33 |
|
---|
34 | public static int activeCount ()
|
---|
35 | {
|
---|
36 | return currentThread().getThreadGroup().activeCount();
|
---|
37 | }
|
---|
38 |
|
---|
39 | public final void checkAccess ()
|
---|
40 | {
|
---|
41 | SecurityManager s = System.getSecurityManager();
|
---|
42 | if (s != null)
|
---|
43 | s.checkAccess(this);
|
---|
44 | }
|
---|
45 |
|
---|
46 | public native int countStackFrames ();
|
---|
47 | public static native Thread currentThread ();
|
---|
48 | public native void destroy ();
|
---|
49 |
|
---|
50 | public static void dumpStack ()
|
---|
51 | {
|
---|
52 | (new Exception ("Stack trace")).printStackTrace ();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static int enumerate (Thread[] threads)
|
---|
56 | {
|
---|
57 | return currentThread().group.enumerate(threads);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public final String getName ()
|
---|
61 | {
|
---|
62 | return name;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public final int getPriority ()
|
---|
66 | {
|
---|
67 | return priority;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public final ThreadGroup getThreadGroup ()
|
---|
71 | {
|
---|
72 | return group;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public native void interrupt ();
|
---|
76 |
|
---|
77 | public static boolean interrupted ()
|
---|
78 | {
|
---|
79 | return currentThread().isInterrupted (true);
|
---|
80 | }
|
---|
81 |
|
---|
82 | // Check the threads interrupted status. Note that this does not clear the
|
---|
83 | // thread's interrupted status (per JDK 1.2 online API documentation).
|
---|
84 | public boolean isInterrupted ()
|
---|
85 | {
|
---|
86 | return interrupt_flag;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public final boolean isAlive ()
|
---|
90 | {
|
---|
91 | return alive_flag;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public final boolean isDaemon ()
|
---|
95 | {
|
---|
96 | return daemon_flag;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public final void join () throws InterruptedException
|
---|
100 | {
|
---|
101 | join (0, 0);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public final void join (long timeout) throws InterruptedException
|
---|
105 | {
|
---|
106 | join (timeout, 0);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public final native void join (long timeout, int nanos)
|
---|
110 | throws InterruptedException;
|
---|
111 |
|
---|
112 | public final native void resume ();
|
---|
113 |
|
---|
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 (!startable_flag)
|
---|
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 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 native void stop (Throwable e);
|
---|
208 | public final native void suspend ();
|
---|
209 |
|
---|
210 | private final native void initialize_native ();
|
---|
211 |
|
---|
212 | private final native static String gen_name ();
|
---|
213 |
|
---|
214 | public Thread (ThreadGroup g, Runnable r, String n)
|
---|
215 | {
|
---|
216 | this (currentThread (), g, r, n);
|
---|
217 |
|
---|
218 | // The Class Libraries book says ``threadName cannot be null''. I
|
---|
219 | // take this to mean NullPointerException.
|
---|
220 | if (n == null)
|
---|
221 | throw new NullPointerException ();
|
---|
222 | }
|
---|
223 |
|
---|
224 | private Thread (Thread current, ThreadGroup g, Runnable r, String n)
|
---|
225 | {
|
---|
226 | if (g == null)
|
---|
227 | {
|
---|
228 | // If CURRENT is null, then we are bootstrapping the first thread.
|
---|
229 | // Use ThreadGroup.root, the main threadgroup.
|
---|
230 | if (current == null)
|
---|
231 | group = ThreadGroup.root;
|
---|
232 | else
|
---|
233 | group = current.getThreadGroup();
|
---|
234 | }
|
---|
235 | else
|
---|
236 | group = g;
|
---|
237 |
|
---|
238 | data = null;
|
---|
239 | interrupt_flag = false;
|
---|
240 | alive_flag = false;
|
---|
241 | startable_flag = true;
|
---|
242 |
|
---|
243 | if (current != null)
|
---|
244 | {
|
---|
245 | group.checkAccess();
|
---|
246 |
|
---|
247 | daemon_flag = current.isDaemon();
|
---|
248 | int gmax = group.getMaxPriority();
|
---|
249 | int pri = current.getPriority();
|
---|
250 | priority = (gmax < pri ? gmax : pri);
|
---|
251 | context_class_loader = current.context_class_loader;
|
---|
252 | InheritableThreadLocal.newChildThread(this);
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | daemon_flag = false;
|
---|
257 | priority = NORM_PRIORITY;
|
---|
258 | }
|
---|
259 |
|
---|
260 | name = n;
|
---|
261 | group.addThread(this);
|
---|
262 | runnable = r;
|
---|
263 |
|
---|
264 | initialize_native ();
|
---|
265 | }
|
---|
266 |
|
---|
267 | public Thread ()
|
---|
268 | {
|
---|
269 | this (null, null, gen_name ());
|
---|
270 | }
|
---|
271 |
|
---|
272 | public Thread (Runnable r)
|
---|
273 | {
|
---|
274 | this (null, r, gen_name ());
|
---|
275 | }
|
---|
276 |
|
---|
277 | public Thread (String n)
|
---|
278 | {
|
---|
279 | this (null, null, n);
|
---|
280 | }
|
---|
281 |
|
---|
282 | public Thread (ThreadGroup g, Runnable r)
|
---|
283 | {
|
---|
284 | this (g, r, gen_name ());
|
---|
285 | }
|
---|
286 |
|
---|
287 | public Thread (ThreadGroup g, String n)
|
---|
288 | {
|
---|
289 | this (g, null, n);
|
---|
290 | }
|
---|
291 |
|
---|
292 | public Thread (Runnable r, String n)
|
---|
293 | {
|
---|
294 | this (null, r, n);
|
---|
295 | }
|
---|
296 |
|
---|
297 | public String toString ()
|
---|
298 | {
|
---|
299 | return "Thread[" + name + "," + priority + "," +
|
---|
300 | (group == null ? "" : group.getName()) + "]";
|
---|
301 | }
|
---|
302 |
|
---|
303 | public static native void yield ();
|
---|
304 |
|
---|
305 | // Private data.
|
---|
306 | ThreadGroup group;
|
---|
307 | String name;
|
---|
308 | private Runnable runnable;
|
---|
309 | private int priority;
|
---|
310 | private boolean daemon_flag;
|
---|
311 | boolean interrupt_flag;
|
---|
312 | private boolean alive_flag;
|
---|
313 | private boolean startable_flag;
|
---|
314 | private ClassLoader context_class_loader;
|
---|
315 |
|
---|
316 | // This describes the top-most interpreter frame for this thread.
|
---|
317 | RawData interp_frame;
|
---|
318 |
|
---|
319 | // Our native data - points to an instance of struct natThread.
|
---|
320 | private Object data;
|
---|
321 | }
|
---|