home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / lang / Thread.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  6.5 KB  |  251 lines

  1. package java.lang;
  2.  
  3. public class Thread implements Runnable {
  4.    private char[] name;
  5.    private int priority;
  6.    private Thread threadQ;
  7.    private int PrivateInfo;
  8.    private int eetop;
  9.    private boolean single_step;
  10.    private boolean daemon = false;
  11.    private boolean stillborn = false;
  12.    private Runnable target;
  13.    private boolean interruptRequested = false;
  14.    private static Thread activeThreadQ;
  15.    private ThreadGroup group;
  16.    private static int threadInitNumber;
  17.    public static final int MIN_PRIORITY = 1;
  18.    public static final int NORM_PRIORITY = 5;
  19.    public static final int MAX_PRIORITY = 10;
  20.  
  21.    private static synchronized int nextThreadNum() {
  22.       return threadInitNumber++;
  23.    }
  24.  
  25.    public static native Thread currentThread();
  26.  
  27.    public static native void yield();
  28.  
  29.    public static native void sleep(long var0) throws InterruptedException;
  30.  
  31.    public static void sleep(long millis, int nanos) throws InterruptedException {
  32.       if (nanos > 500000) {
  33.          ++millis;
  34.       }
  35.  
  36.       sleep(millis);
  37.    }
  38.  
  39.    private void init(ThreadGroup g, Runnable target, String name) {
  40.       Thread parent = currentThread();
  41.       if (g == null) {
  42.          g = parent.getThreadGroup();
  43.       } else {
  44.          g.checkAccess(2);
  45.       }
  46.  
  47.       this.group = g;
  48.       this.daemon = parent.isDaemon();
  49.       this.priority = parent.getPriority();
  50.       this.name = name.toCharArray();
  51.       this.target = target;
  52.       this.setPriority0(this.priority);
  53.       g.add(this);
  54.    }
  55.  
  56.    public Thread() {
  57.       this.init((ThreadGroup)null, (Runnable)null, "Thread-" + nextThreadNum());
  58.    }
  59.  
  60.    public Thread(Runnable target) {
  61.       this.init((ThreadGroup)null, target, "Thread-" + nextThreadNum());
  62.    }
  63.  
  64.    public Thread(ThreadGroup group, Runnable target) {
  65.       this.init(group, target, "Thread-" + nextThreadNum());
  66.    }
  67.  
  68.    public Thread(String name) {
  69.       this.init((ThreadGroup)null, (Runnable)null, name);
  70.    }
  71.  
  72.    public Thread(ThreadGroup group, String name) {
  73.       this.init(group, (Runnable)null, name);
  74.    }
  75.  
  76.    public Thread(Runnable target, String name) {
  77.       this.init((ThreadGroup)null, target, name);
  78.    }
  79.  
  80.    public Thread(ThreadGroup group, Runnable target, String name) {
  81.       this.init(group, target, name);
  82.    }
  83.  
  84.    public synchronized native void start();
  85.  
  86.    public void run() {
  87.       if (this.target != null) {
  88.          this.target.run();
  89.       }
  90.  
  91.    }
  92.  
  93.    private void exit() {
  94.       if (this.group != null) {
  95.          this.group.remove(this);
  96.          this.group = null;
  97.       }
  98.  
  99.    }
  100.  
  101.    public final void stop() {
  102.       SecurityManager.setScopePermission();
  103.       this.stop(new ThreadDeath());
  104.    }
  105.  
  106.    public final synchronized void stop(Throwable o) {
  107.       SecurityManager.checksAccess(this, o, 1);
  108.       this.stop0(o);
  109.    }
  110.  
  111.    public void interrupt() {
  112.       this.interruptRequested = true;
  113.    }
  114.  
  115.    public static boolean interrupted() {
  116.       return currentThread().interruptRequested;
  117.    }
  118.  
  119.    public boolean isInterrupted() {
  120.       return this.interruptRequested;
  121.    }
  122.  
  123.    public void destroy() {
  124.       throw new NoSuchMethodError();
  125.    }
  126.  
  127.    public final native boolean isAlive();
  128.  
  129.    public final void suspend() {
  130.       SecurityManager.checksAccess(this, 1);
  131.       System.err.println("Warning: Thread.suspend() was called; Navigator deadlock might result");
  132.       this.suspend0();
  133.    }
  134.  
  135.    public final void resume() {
  136.       SecurityManager.checksAccess(this, 1);
  137.       this.resume0();
  138.    }
  139.  
  140.    public final void setPriority(int newPriority) {
  141.       try {
  142.          SecurityManager.checksAccess(this, 1);
  143.       } catch (SecurityException var2) {
  144.          return;
  145.       }
  146.  
  147.       if (newPriority <= 10 && newPriority >= 1) {
  148.          if (newPriority > this.group.getMaxPriority()) {
  149.             newPriority = this.group.getMaxPriority();
  150.          }
  151.  
  152.          this.setPriority0(this.priority = newPriority);
  153.       } else {
  154.          throw new IllegalArgumentException();
  155.       }
  156.    }
  157.  
  158.    public final int getPriority() {
  159.       return this.priority;
  160.    }
  161.  
  162.    public final void setName(String name) {
  163.       SecurityManager.checksAccess(this, 1);
  164.       this.name = name.toCharArray();
  165.    }
  166.  
  167.    public final String getName() {
  168.       return String.valueOf(this.name);
  169.    }
  170.  
  171.    public final ThreadGroup getThreadGroup() {
  172.       return this.group;
  173.    }
  174.  
  175.    public static int activeCount() {
  176.       return currentThread().getThreadGroup().activeCount();
  177.    }
  178.  
  179.    public static int enumerate(Thread[] tarray) {
  180.       return currentThread().getThreadGroup().enumerate(tarray);
  181.    }
  182.  
  183.    public native int countStackFrames();
  184.  
  185.    public final synchronized void join(long millis) throws InterruptedException {
  186.       if (millis == 0L) {
  187.          while(this.isAlive()) {
  188.             this.wait(0L);
  189.          }
  190.  
  191.       } else {
  192.          long base = System.currentTimeMillis();
  193.  
  194.          for(long now = 0L; this.isAlive(); now = System.currentTimeMillis() - base) {
  195.             long delay = millis - now;
  196.             if (delay <= 0L) {
  197.                break;
  198.             }
  199.  
  200.             this.wait(delay);
  201.          }
  202.  
  203.       }
  204.    }
  205.  
  206.    public final synchronized void join(long millis, int nanos) throws InterruptedException {
  207.       if (nanos >= 500000 || millis == 0L) {
  208.          ++millis;
  209.       }
  210.  
  211.       this.join(millis);
  212.    }
  213.  
  214.    public final void join() throws InterruptedException {
  215.       this.join(0L);
  216.    }
  217.  
  218.    public static void dumpStack() {
  219.       (new Exception("Stack trace")).printStackTrace();
  220.    }
  221.  
  222.    public final void setDaemon(boolean on) {
  223.       SecurityManager.checksAccess(this, 1);
  224.       if (this.isAlive()) {
  225.          throw new IllegalThreadStateException();
  226.       } else {
  227.          this.daemon = on;
  228.       }
  229.    }
  230.  
  231.    public final boolean isDaemon() {
  232.       return this.daemon;
  233.    }
  234.  
  235.    public void checkAccess() {
  236.       SecurityManager.checksAccess(this, 2);
  237.    }
  238.  
  239.    public String toString() {
  240.       return "Thread[" + this.getName() + "," + this.getPriority() + "," + this.getThreadGroup().getName() + "]";
  241.    }
  242.  
  243.    private native void setPriority0(int var1);
  244.  
  245.    private native void stop0(Object var1);
  246.  
  247.    private native void suspend0();
  248.  
  249.    private native void resume0();
  250. }
  251.