Thread.java 7.44 KB
Newer Older
Tom Tromey committed
1 2
// Thread.java - Thread class.

3
/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
Tom Tromey committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

   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;

/**
 * @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.
21 22 23
 * 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.
Tom Tromey committed
24 25 26 27 28 29 30 31 32 33 34 35 36
 */

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();
  }

37
  public final void checkAccess ()
Tom Tromey committed
38 39 40 41 42 43 44 45 46
  {
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkAccess(this);
  }

  public native int countStackFrames ();
  public static native Thread currentThread ();
  public native void destroy ();
47 48 49 50 51
  
  public static void dumpStack ()
  {
    (new Exception ("Stack trace")).printStackTrace ();
  }
Tom Tromey committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

  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 ()
  {
77
    return currentThread().isInterrupted (true);
Tom Tromey committed
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).
Tom Tromey committed
82 83
  public boolean isInterrupted ()
  {
84
    return interrupt_flag;
Tom Tromey committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  }

  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 ();

  // This method exists only to avoid a warning from the C++ compiler.
113 114
  private static final native void run_ (Object obj);
  private final native void finish_ ();
115

116 117 118
  // 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)
119 120
  {
    boolean r = interrupt_flag;
121 122 123 124 125 126 127
    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;
      }
128 129 130
    return r;
  }
  
Tom Tromey committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144
  public void run ()
  {
    if (runnable != null)
      runnable.run();
  }

  public final void setDaemon (boolean status)
  {
    checkAccess ();
    if (isAlive ())
      throw new IllegalThreadStateException ();
    daemon_flag = status;
  }

Tom Tromey committed
145
  public synchronized ClassLoader getContextClassLoader()
146 147
  {
    if (context_class_loader == null)
Tom Tromey committed
148 149 150 151 152 153
      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)
154
      {
Tom Tromey committed
155 156 157 158 159 160 161 162 163 164 165
	// 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"));
166 167 168 169
      }

    return context_class_loader;
  }
Tom Tromey committed
170

Tom Tromey committed
171
  public synchronized void setContextClassLoader(ClassLoader cl)
172
  {
Tom Tromey committed
173 174 175
    SecurityManager s = System.getSecurityManager ();
    if (s != null)
      s.checkPermission (new RuntimePermission ("setContextClassLoader"));
176 177
    context_class_loader = cl;
  }
Tom Tromey committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

  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;
  public synchronized native void start ();

  public final void stop ()
  {
202 203 204
    // Argument doesn't matter, because this is no longer
    // supported.
    stop (null);
Tom Tromey committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  }

  public final synchronized native void stop (Throwable e);
  public final native void suspend ();

  private final native void initialize_native ();

  private final synchronized static String gen_name ()
  {
    String n;
    n = "Thread-" + nextThreadNumber;
    ++nextThreadNumber;
    return n;
  }

  public Thread (ThreadGroup g, Runnable r, String n)
  {
    Thread current = currentThread ();
223 224
          
    if (g == null)
Tom Tromey committed
225
      {
226 227 228 229 230 231
	// 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();
Tom Tromey committed
232 233
      }
    else
234 235 236
      group = g;
      
    group.checkAccess();
Tom Tromey committed
237 238 239 240 241 242 243

    // The Class Libraries book says ``threadName cannot be null''.  I
    // take this to mean NullPointerException.
    if (n == null)
      throw new NullPointerException ();

    name = n;
244
    group.addThread(this);
Tom Tromey committed
245 246 247 248 249
    runnable = r;

    data = null;
    interrupt_flag = false;
    alive_flag = false;
250
    startable_flag = true;
251

Tom Tromey committed
252 253 254
    if (current != null)
      {
	daemon_flag = current.isDaemon();
255 256 257
        int gmax = group.getMaxPriority();
	int pri = current.getPriority();
	priority = (gmax < pri ? gmax : pri);
258
	context_class_loader = current.context_class_loader;
Tom Tromey committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
      }
    else
      {
	daemon_flag = false;
	priority = NORM_PRIORITY;
      }

    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 ()
  {
301
    return "Thread[" + name + "," + priority + "," + 
302
      (group == null ? "" : group.getName()) + "]";
Tom Tromey committed
303 304 305 306 307 308 309 310 311 312
  }

  public static native void yield ();

  // Private data.
  private ThreadGroup group;
  private String name;
  private Runnable runnable;
  private int priority;
  private boolean daemon_flag;
313
  boolean interrupt_flag;
Tom Tromey committed
314
  private boolean alive_flag;
315
  private boolean startable_flag;
316
  private ClassLoader context_class_loader;
Tom Tromey committed
317

318
  // Our native data.
319
  private Object data;
Tom Tromey committed
320 321 322 323

  // Next thread number to assign.
  private static int nextThreadNumber = 0;
}