]> gcc.gnu.org Git - gcc.git/blob - libjava/java/lang/Thread.java
acinclude.m4 ((GLIBCPP_CHECK_MATH_SUPPORT): Revert last change.
[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: Complete to version 1.1, with caveats
22 * Known problems:
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.
27 */
28
29 public class Thread implements Runnable
30 {
31 public final static int MAX_PRIORITY = 10;
32 public final static int MIN_PRIORITY = 1;
33 public final static int NORM_PRIORITY = 5;
34
35 public static int activeCount ()
36 {
37 return currentThread().getThreadGroup().activeCount();
38 }
39
40 public void checkAccess ()
41 {
42 SecurityManager s = System.getSecurityManager();
43 if (s != null)
44 s.checkAccess(this);
45 }
46
47 public native int countStackFrames ();
48 public static native Thread currentThread ();
49 public native void destroy ();
50
51 public static void dumpStack ()
52 {
53 (new Exception ("Stack trace")).printStackTrace ();
54 }
55
56 public static int enumerate (Thread[] threads)
57 {
58 return currentThread().group.enumerate(threads);
59 }
60
61 public final String getName ()
62 {
63 return name;
64 }
65
66 public final int getPriority ()
67 {
68 return priority;
69 }
70
71 public final ThreadGroup getThreadGroup ()
72 {
73 return group;
74 }
75
76 public native void interrupt ();
77
78 public static boolean interrupted ()
79 {
80 return currentThread().isInterrupted (true);
81 }
82
83 // Check the threads interrupted status. Note that this does not clear the
84 // thread's interrupted status (per JDK 1.2 online API documentation).
85 public boolean isInterrupted ()
86 {
87 return interrupt_flag;
88 }
89
90 public final boolean isAlive ()
91 {
92 return alive_flag;
93 }
94
95 public final boolean isDaemon ()
96 {
97 return daemon_flag;
98 }
99
100 public final void join () throws InterruptedException
101 {
102 join (0, 0);
103 }
104
105 public final void join (long timeout) throws InterruptedException
106 {
107 join (timeout, 0);
108 }
109
110 public final native void join (long timeout, int nanos)
111 throws InterruptedException;
112
113 public final native void resume ();
114
115 // This method exists only to avoid a warning from the C++ compiler.
116 private static final native void run_ (Object obj);
117 private final native void finish_ ();
118
119 // Check the thread's interrupted status. If clear_flag is true, the
120 // thread's interrupted status is also cleared.
121 private boolean isInterrupted (boolean clear_flag)
122 {
123 boolean r = interrupt_flag;
124 if (clear_flag && r)
125 {
126 // Only clear the flag if we saw it as set. Otherwise this could
127 // potentially cause us to miss an interrupt in a race condition,
128 // because this method is not synchronized.
129 interrupt_flag = false;
130 }
131 return r;
132 }
133
134 public void run ()
135 {
136 if (runnable != null)
137 runnable.run();
138 }
139
140 public final void setDaemon (boolean status)
141 {
142 checkAccess ();
143 if (isAlive ())
144 throw new IllegalThreadStateException ();
145 daemon_flag = status;
146 }
147
148 // TODO12:
149 // public ClassLoader getContextClassLoader()
150 // {
151 // }
152
153 // TODO12:
154 // public void setContextClassLoader(ClassLoader cl)
155 // {
156 // }
157
158 public final void setName (String n)
159 {
160 checkAccess ();
161 // The Class Libraries book says ``threadName cannot be null''. I
162 // take this to mean NullPointerException.
163 if (n == null)
164 throw new NullPointerException ();
165 name = n;
166 }
167
168 public final native void setPriority (int newPriority);
169
170 public static void sleep (long timeout) throws InterruptedException
171 {
172 sleep (timeout, 0);
173 }
174
175 public static native void sleep (long timeout, int nanos)
176 throws InterruptedException;
177 public synchronized native void start ();
178
179 public final void stop ()
180 {
181 // Argument doesn't matter, because this is no longer
182 // supported.
183 stop (null);
184 }
185
186 public final synchronized native void stop (Throwable e);
187 public final native void suspend ();
188
189 private final native void initialize_native ();
190
191 private final synchronized static String gen_name ()
192 {
193 String n;
194 n = "Thread-" + nextThreadNumber;
195 ++nextThreadNumber;
196 return n;
197 }
198
199 public Thread (ThreadGroup g, Runnable r, String n)
200 {
201 // Note that CURRENT can be null when we are creating the very
202 // first thread. That's why we check it below.
203 Thread current = currentThread ();
204
205 if (g != null)
206 {
207 // If CURRENT is null, then we are creating the first thread.
208 // In this case we don't do the security check.
209 if (current != null)
210 g.checkAccess();
211 }
212 else
213 g = current.getThreadGroup();
214
215 // The Class Libraries book says ``threadName cannot be null''. I
216 // take this to mean NullPointerException.
217 if (n == null)
218 throw new NullPointerException ();
219
220 name = n;
221 group = g;
222 g.add(this);
223 runnable = r;
224
225 data = null;
226 interrupt_flag = false;
227 alive_flag = false;
228 startable_flag = true;
229
230 if (current != null)
231 {
232 daemon_flag = current.isDaemon();
233 priority = current.getPriority();
234 }
235 else
236 {
237 daemon_flag = false;
238 priority = NORM_PRIORITY;
239 }
240
241 initialize_native ();
242 }
243
244 public Thread ()
245 {
246 this (null, null, gen_name ());
247 }
248
249 public Thread (Runnable r)
250 {
251 this (null, r, gen_name ());
252 }
253
254 public Thread (String n)
255 {
256 this (null, null, n);
257 }
258
259 public Thread (ThreadGroup g, Runnable r)
260 {
261 this (g, r, gen_name ());
262 }
263
264 public Thread (ThreadGroup g, String n)
265 {
266 this (g, null, n);
267 }
268
269 public Thread (Runnable r, String n)
270 {
271 this (null, r, n);
272 }
273
274 public String toString ()
275 {
276 return "Thread[" + name + "," + priority + "," +
277 (group == null ? "" : group.getName()) + "]";
278 }
279
280 public static native void yield ();
281
282 // Private data.
283 private ThreadGroup group;
284 private String name;
285 private Runnable runnable;
286 private int priority;
287 private boolean daemon_flag;
288 private boolean interrupt_flag;
289 private boolean alive_flag;
290 private boolean startable_flag;
291
292 // Our native data.
293 private Object data;
294
295 // Next thread number to assign.
296 private static int nextThreadNumber = 0;
297 }
This page took 0.050013 seconds and 5 git commands to generate.