]>
gcc.gnu.org Git - gcc.git/blob - libjava/java/lang/Thread.java
1 // Thread.java - Thread class.
3 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation
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 ();
51 public static void dumpStack ()
53 (new Exception ("Stack trace")).printStackTrace ();
56 public static int enumerate (Thread
[] threads
)
58 return currentThread().group
.enumerate(threads
);
61 public final String
getName ()
66 public final int getPriority ()
71 public final ThreadGroup
getThreadGroup ()
76 public native void interrupt ();
78 public static boolean interrupted ()
80 return currentThread().isInterrupted (true);
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 ()
87 return interrupt_flag
;
90 public final boolean isAlive ()
95 public final boolean isDaemon ()
100 public final void join () throws InterruptedException
105 public final void join (long timeout
) throws InterruptedException
110 public final native void join (long timeout
, int nanos
)
111 throws InterruptedException
;
113 public final native void resume ();
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_ ();
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
)
123 boolean r
= interrupt_flag
;
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;
136 if (runnable
!= null)
140 public final void setDaemon (boolean status
)
144 throw new IllegalThreadStateException ();
145 daemon_flag
= status
;
149 // public ClassLoader getContextClassLoader()
154 // public void setContextClassLoader(ClassLoader cl)
158 public final void setName (String n
)
161 // The Class Libraries book says ``threadName cannot be null''. I
162 // take this to mean NullPointerException.
164 throw new NullPointerException ();
168 public final native void setPriority (int newPriority
);
170 public static void sleep (long timeout
) throws InterruptedException
175 public static native void sleep (long timeout
, int nanos
)
176 throws InterruptedException
;
177 public synchronized native void start ();
179 public final void stop ()
181 // Argument doesn't matter, because this is no longer
186 public final synchronized native void stop (Throwable e
);
187 public final native void suspend ();
189 private final native void initialize_native ();
191 private final synchronized static String
gen_name ()
194 n
= "Thread-" + nextThreadNumber
;
199 public Thread (ThreadGroup g
, Runnable r
, String n
)
201 Thread current
= currentThread ();
205 // If CURRENT is null, then we are bootstrapping the first thread.
206 // Use ThreadGroup.root, the main threadgroup.
208 group
= ThreadGroup
.root
;
210 group
= current
.getThreadGroup();
217 // The Class Libraries book says ``threadName cannot be null''. I
218 // take this to mean NullPointerException.
220 throw new NullPointerException ();
227 interrupt_flag
= false;
229 startable_flag
= true;
233 daemon_flag
= current
.isDaemon();
234 int gmax
= group
.getMaxPriority();
235 int pri
= current
.getPriority();
236 priority
= (gmax
< pri ? gmax
: pri
);
241 priority
= NORM_PRIORITY
;
244 initialize_native ();
249 this (null, null, gen_name ());
252 public Thread (Runnable r
)
254 this (null, r
, gen_name ());
257 public Thread (String n
)
259 this (null, null, n
);
262 public Thread (ThreadGroup g
, Runnable r
)
264 this (g
, r
, gen_name ());
267 public Thread (ThreadGroup g
, String n
)
272 public Thread (Runnable r
, String n
)
277 public String
toString ()
279 return "Thread[" + name
+ "," + priority
+ "," +
280 (group
== null ?
"" : group
.getName()) + "]";
283 public static native void yield ();
286 private ThreadGroup group
;
288 private Runnable runnable
;
289 private int priority
;
290 private boolean daemon_flag
;
291 private boolean interrupt_flag
;
292 private boolean alive_flag
;
293 private boolean startable_flag
;
298 // Next thread number to assign.
299 private static int nextThreadNumber
= 0;
This page took 0.184682 seconds and 5 git commands to generate.