home *** CD-ROM | disk | FTP | other *** search
/ Performance Now / PFE4.iso / worldnet / ns3230d / disk2 / java.z / java_30 / java / lang / ThreadGroup.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-08-15  |  6.7 KB  |  358 lines

  1. package java.lang;
  2.  
  3. import java.io.PrintStream;
  4.  
  5. public class ThreadGroup {
  6.    ThreadGroup parent;
  7.    String name;
  8.    int maxPriority;
  9.    boolean destroyed;
  10.    boolean daemon;
  11.    int nthreads;
  12.    Thread[] threads;
  13.    int ngroups;
  14.    ThreadGroup[] groups;
  15.  
  16.    private ThreadGroup() {
  17.       this.name = "system";
  18.       this.maxPriority = 10;
  19.    }
  20.  
  21.    public ThreadGroup(String name) {
  22.       this(Thread.currentThread().getThreadGroup(), name);
  23.    }
  24.  
  25.    public ThreadGroup(ThreadGroup parent, String name) {
  26.       if (parent == null) {
  27.          throw new NullPointerException();
  28.       } else {
  29.          parent.checkAccess(1);
  30.          this.name = name;
  31.          this.maxPriority = parent.maxPriority;
  32.          this.daemon = parent.daemon;
  33.          this.parent = parent;
  34.          parent.add(this);
  35.       }
  36.    }
  37.  
  38.    public final String getName() {
  39.       return this.name;
  40.    }
  41.  
  42.    public final ThreadGroup getParent() {
  43.       return this.parent;
  44.    }
  45.  
  46.    public final int getMaxPriority() {
  47.       return this.maxPriority;
  48.    }
  49.  
  50.    public final boolean isDaemon() {
  51.       return this.daemon;
  52.    }
  53.  
  54.    public final void setDaemon(boolean daemon) {
  55.       SecurityManager.checksAccess(this, 1);
  56.       this.daemon = daemon;
  57.    }
  58.  
  59.    public final synchronized void setMaxPriority(int pri) {
  60.       SecurityManager.checksAccess(this, 1);
  61.       if (pri < 1) {
  62.          this.maxPriority = 1;
  63.       } else if (pri < this.maxPriority) {
  64.          this.maxPriority = pri;
  65.       }
  66.  
  67.       for(int i = 0; i < this.ngroups; ++i) {
  68.          this.groups[i].setMaxPriority(pri);
  69.       }
  70.  
  71.    }
  72.  
  73.    public final boolean parentOf(ThreadGroup g) {
  74.       while(g != null) {
  75.          if (g == this) {
  76.             return true;
  77.          }
  78.  
  79.          g = g.parent;
  80.       }
  81.  
  82.       return false;
  83.    }
  84.  
  85.    public final void checkAccess(int caller_depth) {
  86.       SecurityManager.checksAccess(this, caller_depth + 1);
  87.    }
  88.  
  89.    public synchronized int activeCount() {
  90.       if (this.destroyed) {
  91.          return 0;
  92.       } else {
  93.          int n = this.nthreads;
  94.  
  95.          for(int i = 0; i < this.ngroups; ++i) {
  96.             n += this.groups[i].activeCount();
  97.          }
  98.  
  99.          return n;
  100.       }
  101.    }
  102.  
  103.    public int enumerate(Thread[] list) {
  104.       return this.enumerate((Thread[])list, 0, true);
  105.    }
  106.  
  107.    public int enumerate(Thread[] list, boolean recurse) {
  108.       return this.enumerate((Thread[])list, 0, recurse);
  109.    }
  110.  
  111.    private synchronized int enumerate(Thread[] list, int n, boolean recurse) {
  112.       if (this.destroyed) {
  113.          return 0;
  114.       } else {
  115.          int nt = this.nthreads;
  116.          if (nt > list.length - n) {
  117.             nt = list.length - n;
  118.          }
  119.  
  120.          if (nt > 0) {
  121.             System.arraycopy(this.threads, 0, list, n, nt);
  122.             n += nt;
  123.          }
  124.  
  125.          if (recurse) {
  126.             for(int i = 0; i < this.ngroups; ++i) {
  127.                n = this.groups[i].enumerate(list, n, true);
  128.             }
  129.          }
  130.  
  131.          return n;
  132.       }
  133.    }
  134.  
  135.    public synchronized int activeGroupCount() {
  136.       if (this.destroyed) {
  137.          return 0;
  138.       } else {
  139.          int n = this.ngroups;
  140.  
  141.          for(int i = 0; i < this.ngroups; ++i) {
  142.             n += this.groups[i].activeGroupCount();
  143.          }
  144.  
  145.          return n;
  146.       }
  147.    }
  148.  
  149.    public int enumerate(ThreadGroup[] list) {
  150.       return this.enumerate((ThreadGroup[])list, 0, true);
  151.    }
  152.  
  153.    public int enumerate(ThreadGroup[] list, boolean recurse) {
  154.       return this.enumerate((ThreadGroup[])list, 0, recurse);
  155.    }
  156.  
  157.    private synchronized int enumerate(ThreadGroup[] list, int n, boolean recurse) {
  158.       if (this.destroyed) {
  159.          return 0;
  160.       } else {
  161.          int ng = this.ngroups;
  162.          if (ng > list.length - n) {
  163.             ng = list.length - n;
  164.          }
  165.  
  166.          if (ng > 0) {
  167.             System.arraycopy(this.groups, 0, list, n, ng);
  168.             n += ng;
  169.          }
  170.  
  171.          if (recurse) {
  172.             for(int i = 0; i < this.ngroups; ++i) {
  173.                n = this.groups[i].enumerate(list, n, true);
  174.             }
  175.          }
  176.  
  177.          return n;
  178.       }
  179.    }
  180.  
  181.    public final synchronized void stop() {
  182.       SecurityManager.checksAccess(this, 1);
  183.  
  184.       for(int i = 0; i < this.ngroups; ++i) {
  185.          this.groups[i].stop();
  186.       }
  187.  
  188.       for(int i = 0; i < this.nthreads; ++i) {
  189.          this.threads[i].stop();
  190.       }
  191.  
  192.    }
  193.  
  194.    public final synchronized void suspend() {
  195.       SecurityManager.checksAccess(this, 1);
  196.  
  197.       for(int i = 0; i < this.ngroups; ++i) {
  198.          this.groups[i].suspend();
  199.       }
  200.  
  201.       for(int i = 0; i < this.nthreads; ++i) {
  202.          this.threads[i].suspend();
  203.       }
  204.  
  205.    }
  206.  
  207.    public final synchronized void resume() {
  208.       SecurityManager.checksAccess(this, 1);
  209.  
  210.       for(int i = 0; i < this.ngroups; ++i) {
  211.          this.groups[i].resume();
  212.       }
  213.  
  214.       for(int i = 0; i < this.nthreads; ++i) {
  215.          this.threads[i].resume();
  216.       }
  217.  
  218.    }
  219.  
  220.    public final synchronized void destroy() {
  221.       SecurityManager.checksAccess(this, 1);
  222.       if (!this.destroyed && this.nthreads <= 0) {
  223.          while(this.ngroups > 0) {
  224.             this.groups[0].destroy();
  225.          }
  226.  
  227.          if (this.parent != null) {
  228.             this.destroyed = true;
  229.             this.groups = null;
  230.             this.threads = null;
  231.             this.parent.remove(this);
  232.          }
  233.  
  234.       } else {
  235.          throw new IllegalThreadStateException();
  236.       }
  237.    }
  238.  
  239.    private final synchronized void add(ThreadGroup g) {
  240.       if (this.destroyed) {
  241.          throw new IllegalThreadStateException();
  242.       } else {
  243.          SecurityManager.setScopePermission();
  244.          if (this.groups == null) {
  245.             this.groups = new ThreadGroup[4];
  246.          } else if (this.ngroups == this.groups.length) {
  247.             ThreadGroup[] newgroups = new ThreadGroup[this.ngroups * 2];
  248.             System.arraycopy(this.groups, 0, newgroups, 0, this.ngroups);
  249.             this.groups = newgroups;
  250.          }
  251.  
  252.          this.groups[this.ngroups] = g;
  253.          ++this.ngroups;
  254.       }
  255.    }
  256.  
  257.    private synchronized void remove(ThreadGroup g) {
  258.       if (!this.destroyed) {
  259.          for(int i = 0; i < this.ngroups; ++i) {
  260.             if (this.groups[i] == g) {
  261.                System.arraycopy(this.groups, i + 1, this.groups, i, --this.ngroups - i);
  262.                this.groups[this.ngroups] = null;
  263.                break;
  264.             }
  265.          }
  266.  
  267.          if (this.nthreads == 0) {
  268.             this.notifyAll();
  269.          }
  270.  
  271.          if (this.daemon && this.nthreads == 0 && this.ngroups == 0) {
  272.             SecurityManager.setScopePermission();
  273.             this.destroy();
  274.          }
  275.  
  276.       }
  277.    }
  278.  
  279.    synchronized void add(Thread t) {
  280.       if (this.destroyed) {
  281.          throw new IllegalThreadStateException();
  282.       } else {
  283.          if (this.threads == null) {
  284.             this.threads = new Thread[4];
  285.          } else if (this.nthreads == this.threads.length) {
  286.             Thread[] newthreads = new Thread[this.nthreads * 2];
  287.             System.arraycopy(this.threads, 0, newthreads, 0, this.nthreads);
  288.             this.threads = newthreads;
  289.          }
  290.  
  291.          this.threads[this.nthreads] = t;
  292.          ++this.nthreads;
  293.       }
  294.    }
  295.  
  296.    synchronized void remove(Thread t) {
  297.       if (!this.destroyed) {
  298.          for(int i = 0; i < this.nthreads; ++i) {
  299.             if (this.threads[i] == t) {
  300.                System.arraycopy(this.threads, i + 1, this.threads, i, --this.nthreads - i);
  301.                this.threads[this.nthreads] = null;
  302.                break;
  303.             }
  304.          }
  305.  
  306.          if (this.nthreads == 0) {
  307.             this.notifyAll();
  308.          }
  309.  
  310.          if (this.daemon && this.nthreads == 0 && this.ngroups == 0) {
  311.             this.destroy();
  312.          }
  313.  
  314.       }
  315.    }
  316.  
  317.    public synchronized void list() {
  318.       this.list(System.out, 0);
  319.    }
  320.  
  321.    void list(PrintStream out, int indent) {
  322.       for(int j = 0; j < indent; ++j) {
  323.          out.print(" ");
  324.       }
  325.  
  326.       out.println(this);
  327.       indent += 4;
  328.  
  329.       for(int i = 0; i < this.nthreads; ++i) {
  330.          for(int j = 0; j < indent; ++j) {
  331.             out.print(" ");
  332.          }
  333.  
  334.          out.println(this.threads[i]);
  335.       }
  336.  
  337.       for(int i = 0; i < this.ngroups; ++i) {
  338.          this.groups[i].list(out, indent);
  339.       }
  340.  
  341.    }
  342.  
  343.    public void uncaughtException(Thread t, Throwable e) {
  344.       if (this.parent != null) {
  345.          this.parent.uncaughtException(t, e);
  346.       } else {
  347.          if (!(e instanceof ThreadDeath)) {
  348.             e.printStackTrace(System.err);
  349.          }
  350.  
  351.       }
  352.    }
  353.  
  354.    public String toString() {
  355.       return this.getClass().getName() + "[name=" + this.getName() + ",maxpri=" + this.maxPriority + "]";
  356.    }
  357. }
  358.