1999-04-07 22:42:40 +08:00
|
|
|
// Thread.java - Thread class.
|
|
|
|
|
2002-01-05 04:38:20 +08:00
|
|
|
/* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
package java.lang;
|
|
|
|
|
2002-08-30 01:53:28 +08:00
|
|
|
import gnu.gcj.RawData;
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
/**
|
|
|
|
* @author Tom Tromey <tromey@cygnus.com>
|
|
|
|
* @date August 24, 1998
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
|
|
|
* "The Java Language Specification", ISBN 0-201-63451-1
|
|
|
|
* plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
|
2001-01-05 08:31:45 +08:00
|
|
|
* Status: Believed complete to version 1.3, with caveats. We do not
|
|
|
|
* implement the deprecated (and dangerous) stop, suspend, and resume
|
|
|
|
* methods. Security implementation is not complete.
|
1999-04-07 22:42:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class Thread implements Runnable
|
|
|
|
{
|
|
|
|
public final static int MAX_PRIORITY = 10;
|
|
|
|
public final static int MIN_PRIORITY = 1;
|
|
|
|
public final static int NORM_PRIORITY = 5;
|
|
|
|
|
|
|
|
public static int activeCount ()
|
|
|
|
{
|
|
|
|
return currentThread().getThreadGroup().activeCount();
|
|
|
|
}
|
|
|
|
|
2000-12-15 14:15:49 +08:00
|
|
|
public final void checkAccess ()
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
|
|
|
SecurityManager s = System.getSecurityManager();
|
|
|
|
if (s != null)
|
|
|
|
s.checkAccess(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public native int countStackFrames ();
|
|
|
|
public static native Thread currentThread ();
|
|
|
|
public native void destroy ();
|
2000-02-15 16:47:16 +08:00
|
|
|
|
|
|
|
public static void dumpStack ()
|
|
|
|
{
|
|
|
|
(new Exception ("Stack trace")).printStackTrace ();
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public static int enumerate (Thread[] threads)
|
|
|
|
{
|
|
|
|
return currentThread().group.enumerate(threads);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final String getName ()
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int getPriority ()
|
|
|
|
{
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final ThreadGroup getThreadGroup ()
|
|
|
|
{
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
public native void interrupt ();
|
|
|
|
|
|
|
|
public static boolean interrupted ()
|
|
|
|
{
|
2000-03-28 10:22:24 +08:00
|
|
|
return currentThread().isInterrupted (true);
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
1999-12-27 15:33:22 +08:00
|
|
|
// Check the threads interrupted status. Note that this does not clear the
|
2000-03-28 10:22:24 +08:00
|
|
|
// thread's interrupted status (per JDK 1.2 online API documentation).
|
1999-04-07 22:42:40 +08:00
|
|
|
public boolean isInterrupted ()
|
|
|
|
{
|
1999-12-27 15:33:22 +08:00
|
|
|
return interrupt_flag;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public final boolean isAlive ()
|
|
|
|
{
|
|
|
|
return alive_flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final boolean isDaemon ()
|
|
|
|
{
|
|
|
|
return daemon_flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final void join () throws InterruptedException
|
|
|
|
{
|
|
|
|
join (0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final void join (long timeout) throws InterruptedException
|
|
|
|
{
|
|
|
|
join (timeout, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final native void join (long timeout, int nanos)
|
|
|
|
throws InterruptedException;
|
|
|
|
|
|
|
|
public final native void resume ();
|
|
|
|
|
2000-03-03 04:25:20 +08:00
|
|
|
private final native void finish_ ();
|
1999-12-27 15:33:22 +08:00
|
|
|
|
2000-03-28 10:22:24 +08:00
|
|
|
// Check the thread's interrupted status. If clear_flag is true, the
|
|
|
|
// thread's interrupted status is also cleared.
|
|
|
|
private boolean isInterrupted (boolean clear_flag)
|
1999-12-27 15:33:22 +08:00
|
|
|
{
|
|
|
|
boolean r = interrupt_flag;
|
2000-03-28 10:22:24 +08:00
|
|
|
if (clear_flag && r)
|
|
|
|
{
|
|
|
|
// Only clear the flag if we saw it as set. Otherwise this could
|
|
|
|
// potentially cause us to miss an interrupt in a race condition,
|
|
|
|
// because this method is not synchronized.
|
|
|
|
interrupt_flag = false;
|
|
|
|
}
|
1999-12-27 15:33:22 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
public void run ()
|
|
|
|
{
|
|
|
|
if (runnable != null)
|
|
|
|
runnable.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public final void setDaemon (boolean status)
|
|
|
|
{
|
|
|
|
checkAccess ();
|
|
|
|
if (isAlive ())
|
|
|
|
throw new IllegalThreadStateException ();
|
|
|
|
daemon_flag = status;
|
|
|
|
}
|
|
|
|
|
2000-11-27 12:07:48 +08:00
|
|
|
public synchronized ClassLoader getContextClassLoader()
|
2000-11-26 11:58:56 +08:00
|
|
|
{
|
|
|
|
if (context_class_loader == null)
|
2000-11-27 12:07:48 +08:00
|
|
|
context_class_loader = ClassLoader.getSystemClassLoader ();
|
|
|
|
|
|
|
|
SecurityManager s = System.getSecurityManager();
|
|
|
|
// FIXME: we can't currently find the caller's class loader.
|
|
|
|
ClassLoader callers = null;
|
|
|
|
if (s != null && callers != null)
|
2000-11-26 11:58:56 +08:00
|
|
|
{
|
2000-11-27 12:07:48 +08:00
|
|
|
// See if the caller's class loader is the same as or an
|
|
|
|
// ancestor of this thread's class loader.
|
|
|
|
while (callers != null && callers != context_class_loader)
|
|
|
|
{
|
|
|
|
// FIXME: should use some internal version of getParent
|
|
|
|
// that avoids security checks.
|
|
|
|
callers = callers.getParent ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callers != context_class_loader)
|
|
|
|
s.checkPermission (new RuntimePermission ("getClassLoader"));
|
2000-11-26 11:58:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return context_class_loader;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
2000-11-27 12:07:48 +08:00
|
|
|
public synchronized void setContextClassLoader(ClassLoader cl)
|
2000-11-26 11:58:56 +08:00
|
|
|
{
|
2000-11-27 12:07:48 +08:00
|
|
|
SecurityManager s = System.getSecurityManager ();
|
|
|
|
if (s != null)
|
|
|
|
s.checkPermission (new RuntimePermission ("setContextClassLoader"));
|
2000-11-26 11:58:56 +08:00
|
|
|
context_class_loader = cl;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public final void setName (String n)
|
|
|
|
{
|
|
|
|
checkAccess ();
|
|
|
|
// The Class Libraries book says ``threadName cannot be null''. I
|
|
|
|
// take this to mean NullPointerException.
|
|
|
|
if (n == null)
|
|
|
|
throw new NullPointerException ();
|
|
|
|
name = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final native void setPriority (int newPriority);
|
|
|
|
|
|
|
|
public static void sleep (long timeout) throws InterruptedException
|
|
|
|
{
|
|
|
|
sleep (timeout, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static native void sleep (long timeout, int nanos)
|
|
|
|
throws InterruptedException;
|
2002-01-05 04:38:20 +08:00
|
|
|
public native void start ();
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public final void stop ()
|
|
|
|
{
|
1999-11-05 00:45:11 +08:00
|
|
|
// Argument doesn't matter, because this is no longer
|
|
|
|
// supported.
|
|
|
|
stop (null);
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
2002-01-05 04:38:20 +08:00
|
|
|
public final native void stop (Throwable e);
|
1999-04-07 22:42:40 +08:00
|
|
|
public final native void suspend ();
|
|
|
|
|
|
|
|
private final native void initialize_native ();
|
|
|
|
|
2001-05-22 14:47:48 +08:00
|
|
|
private final native static String gen_name ();
|
|
|
|
|
|
|
|
public Thread (ThreadGroup g, Runnable r, String n)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
2001-05-22 14:47:48 +08:00
|
|
|
this (currentThread (), g, r, n);
|
|
|
|
|
|
|
|
// The Class Libraries book says ``threadName cannot be null''. I
|
|
|
|
// take this to mean NullPointerException.
|
|
|
|
if (n == null)
|
|
|
|
throw new NullPointerException ();
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
2001-05-22 14:47:48 +08:00
|
|
|
private Thread (Thread current, ThreadGroup g, Runnable r, String n)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
2000-06-20 21:30:14 +08:00
|
|
|
if (g == null)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
2000-06-20 21:30:14 +08:00
|
|
|
// If CURRENT is null, then we are bootstrapping the first thread.
|
|
|
|
// Use ThreadGroup.root, the main threadgroup.
|
|
|
|
if (current == null)
|
|
|
|
group = ThreadGroup.root;
|
|
|
|
else
|
|
|
|
group = current.getThreadGroup();
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
else
|
2000-06-20 21:30:14 +08:00
|
|
|
group = g;
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
data = null;
|
|
|
|
interrupt_flag = false;
|
|
|
|
alive_flag = false;
|
2000-03-28 10:22:24 +08:00
|
|
|
startable_flag = true;
|
2000-11-26 11:58:56 +08:00
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
if (current != null)
|
|
|
|
{
|
2001-05-22 14:47:48 +08:00
|
|
|
group.checkAccess();
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
daemon_flag = current.isDaemon();
|
2000-06-20 21:30:14 +08:00
|
|
|
int gmax = group.getMaxPriority();
|
|
|
|
int pri = current.getPriority();
|
|
|
|
priority = (gmax < pri ? gmax : pri);
|
2000-11-26 11:58:56 +08:00
|
|
|
context_class_loader = current.context_class_loader;
|
2001-10-03 06:49:59 +08:00
|
|
|
InheritableThreadLocal.newChildThread(this);
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
daemon_flag = false;
|
|
|
|
priority = NORM_PRIORITY;
|
|
|
|
}
|
|
|
|
|
2001-05-22 14:47:48 +08:00
|
|
|
name = n;
|
|
|
|
group.addThread(this);
|
|
|
|
runnable = r;
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
initialize_native ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Thread ()
|
|
|
|
{
|
|
|
|
this (null, null, gen_name ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Thread (Runnable r)
|
|
|
|
{
|
|
|
|
this (null, r, gen_name ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Thread (String n)
|
|
|
|
{
|
|
|
|
this (null, null, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Thread (ThreadGroup g, Runnable r)
|
|
|
|
{
|
|
|
|
this (g, r, gen_name ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Thread (ThreadGroup g, String n)
|
|
|
|
{
|
|
|
|
this (g, null, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Thread (Runnable r, String n)
|
|
|
|
{
|
|
|
|
this (null, r, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString ()
|
|
|
|
{
|
2000-03-28 10:22:24 +08:00
|
|
|
return "Thread[" + name + "," + priority + "," +
|
2000-11-26 11:58:56 +08:00
|
|
|
(group == null ? "" : group.getName()) + "]";
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static native void yield ();
|
|
|
|
|
|
|
|
// Private data.
|
2002-06-18 23:40:16 +08:00
|
|
|
ThreadGroup group;
|
|
|
|
String name;
|
1999-04-07 22:42:40 +08:00
|
|
|
private Runnable runnable;
|
|
|
|
private int priority;
|
|
|
|
private boolean daemon_flag;
|
2000-09-07 20:09:41 +08:00
|
|
|
boolean interrupt_flag;
|
1999-04-07 22:42:40 +08:00
|
|
|
private boolean alive_flag;
|
2000-03-28 10:22:24 +08:00
|
|
|
private boolean startable_flag;
|
2000-11-26 11:58:56 +08:00
|
|
|
private ClassLoader context_class_loader;
|
1999-04-07 22:42:40 +08:00
|
|
|
|
2002-08-30 01:53:28 +08:00
|
|
|
// This describes the top-most interpreter frame for this thread.
|
|
|
|
RawData interp_frame;
|
|
|
|
|
2001-05-22 14:47:48 +08:00
|
|
|
// Our native data - points to an instance of struct natThread.
|
2000-05-18 18:08:25 +08:00
|
|
|
private Object data;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|