home *** CD-ROM | disk | FTP | other *** search
/ Print Shop Ensemble 3 / the-print-shop-ensemble-iii.iso / worldnet / disk2 / java.z / MOZ2_01.ZIP / java / lang / ThreadGroup.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-03-08  |  6.6 KB  |  344 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();
  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.       this.checkAccess();
  56.       this.daemon = daemon;
  57.    }
  58.  
  59.    public final synchronized void setMaxPriority(int pri) {
  60.       this.checkAccess();
  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(i);
  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() {
  86.       SecurityManager security = System.getSecurityManager();
  87.       if (security != null) {
  88.          security.checkAccess(this);
  89.       }
  90.  
  91.    }
  92.  
  93.    public synchronized int activeCount() {
  94.       int n = this.nthreads;
  95.  
  96.       for(int i = 0; i < this.ngroups; ++i) {
  97.          n += this.groups[i].activeCount();
  98.       }
  99.  
  100.       return n;
  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.       int nt = this.nthreads;
  113.       if (nt > list.length - n) {
  114.          nt = list.length - n;
  115.       }
  116.  
  117.       if (nt > 0) {
  118.          System.arraycopy(this.threads, 0, list, n, nt);
  119.          n += nt;
  120.       }
  121.  
  122.       if (recurse) {
  123.          for(int i = 0; i < this.ngroups; ++i) {
  124.             n = this.groups[i].enumerate(list, n, true);
  125.          }
  126.       }
  127.  
  128.       return n;
  129.    }
  130.  
  131.    public synchronized int activeGroupCount() {
  132.       int n = this.ngroups;
  133.  
  134.       for(int i = 0; i < this.ngroups; ++i) {
  135.          n += this.groups[i].activeGroupCount();
  136.       }
  137.  
  138.       return n;
  139.    }
  140.  
  141.    public int enumerate(ThreadGroup[] list) {
  142.       return this.enumerate((ThreadGroup[])list, 0, true);
  143.    }
  144.  
  145.    public int enumerate(ThreadGroup[] list, boolean recurse) {
  146.       return this.enumerate((ThreadGroup[])list, 0, recurse);
  147.    }
  148.  
  149.    private synchronized int enumerate(ThreadGroup[] list, int n, boolean recurse) {
  150.       int ng = this.ngroups;
  151.       if (ng > list.length - n) {
  152.          ng = list.length - n;
  153.       }
  154.  
  155.       if (ng > 0) {
  156.          System.arraycopy(this.groups, 0, list, n, ng);
  157.          n += ng;
  158.       }
  159.  
  160.       if (recurse) {
  161.          for(int i = 0; i < this.ngroups; ++i) {
  162.             n = this.groups[i].enumerate(list, n, true);
  163.          }
  164.       }
  165.  
  166.       return n;
  167.    }
  168.  
  169.    public final synchronized void stop() {
  170.       this.checkAccess();
  171.  
  172.       for(int i = 0; i < this.ngroups; ++i) {
  173.          this.groups[i].stop();
  174.       }
  175.  
  176.       for(int i = 0; i < this.nthreads; ++i) {
  177.          this.threads[i].stop();
  178.       }
  179.  
  180.    }
  181.  
  182.    public final synchronized void suspend() {
  183.       this.checkAccess();
  184.  
  185.       for(int i = 0; i < this.ngroups; ++i) {
  186.          this.groups[i].suspend();
  187.       }
  188.  
  189.       for(int i = 0; i < this.nthreads; ++i) {
  190.          this.threads[i].suspend();
  191.       }
  192.  
  193.    }
  194.  
  195.    public final synchronized void resume() {
  196.       this.checkAccess();
  197.  
  198.       for(int i = 0; i < this.ngroups; ++i) {
  199.          this.groups[i].resume();
  200.       }
  201.  
  202.       for(int i = 0; i < this.nthreads; ++i) {
  203.          this.threads[i].resume();
  204.       }
  205.  
  206.    }
  207.  
  208.    public final synchronized void destroy() {
  209.       this.checkAccess();
  210.       if (!this.destroyed && this.nthreads <= 0) {
  211.          while(this.ngroups > 0) {
  212.             this.groups[0].destroy();
  213.          }
  214.  
  215.          if (this.parent != null) {
  216.             this.destroyed = true;
  217.             this.groups = null;
  218.             this.threads = null;
  219.             this.parent.remove(this);
  220.          }
  221.  
  222.       } else {
  223.          throw new IllegalThreadStateException();
  224.       }
  225.    }
  226.  
  227.    private final synchronized void add(ThreadGroup g) {
  228.       if (this.destroyed) {
  229.          throw new IllegalThreadStateException();
  230.       } else {
  231.          if (this.groups == null) {
  232.             this.groups = new ThreadGroup[4];
  233.          } else if (this.ngroups == this.groups.length) {
  234.             ThreadGroup[] newgroups = new ThreadGroup[this.ngroups * 2];
  235.             System.arraycopy(this.groups, 0, newgroups, 0, this.ngroups);
  236.             this.groups = newgroups;
  237.          }
  238.  
  239.          this.groups[this.ngroups] = g;
  240.          ++this.ngroups;
  241.       }
  242.    }
  243.  
  244.    private synchronized void remove(ThreadGroup g) {
  245.       if (!this.destroyed) {
  246.          for(int i = 0; i < this.ngroups; ++i) {
  247.             if (this.groups[i] == g) {
  248.                System.arraycopy(this.groups, i + 1, this.groups, i, --this.ngroups - i);
  249.                this.groups[this.ngroups] = null;
  250.                break;
  251.             }
  252.          }
  253.  
  254.          if (this.nthreads == 0) {
  255.             this.notifyAll();
  256.          }
  257.  
  258.          if (this.daemon && this.nthreads == 0 && this.ngroups == 0) {
  259.             this.destroy();
  260.          }
  261.  
  262.       }
  263.    }
  264.  
  265.    synchronized void add(Thread t) {
  266.       if (this.destroyed) {
  267.          throw new IllegalThreadStateException();
  268.       } else {
  269.          if (this.threads == null) {
  270.             this.threads = new Thread[4];
  271.          } else if (this.nthreads == this.threads.length) {
  272.             Thread[] newthreads = new Thread[this.nthreads * 2];
  273.             System.arraycopy(this.threads, 0, newthreads, 0, this.nthreads);
  274.             this.threads = newthreads;
  275.          }
  276.  
  277.          this.threads[this.nthreads] = t;
  278.          ++this.nthreads;
  279.       }
  280.    }
  281.  
  282.    synchronized void remove(Thread t) {
  283.       if (!this.destroyed) {
  284.          for(int i = 0; i < this.nthreads; ++i) {
  285.             if (this.threads[i] == t) {
  286.                System.arraycopy(this.threads, i + 1, this.threads, i, --this.nthreads - i);
  287.                this.threads[this.nthreads] = null;
  288.                break;
  289.             }
  290.          }
  291.  
  292.          if (this.nthreads == 0) {
  293.             this.notifyAll();
  294.          }
  295.  
  296.          if (this.daemon && this.nthreads == 0 && this.ngroups == 0) {
  297.             this.destroy();
  298.          }
  299.  
  300.       }
  301.    }
  302.  
  303.    public synchronized void list() {
  304.       this.list(System.out, 0);
  305.    }
  306.  
  307.    void list(PrintStream out, int indent) {
  308.       for(int j = 0; j < indent; ++j) {
  309.          out.print(" ");
  310.       }
  311.  
  312.       out.println(this);
  313.       indent += 4;
  314.  
  315.       for(int i = 0; i < this.nthreads; ++i) {
  316.          for(int j = 0; j < indent; ++j) {
  317.             out.print(" ");
  318.          }
  319.  
  320.          out.println(this.threads[i]);
  321.       }
  322.  
  323.       for(int i = 0; i < this.ngroups; ++i) {
  324.          this.groups[i].list(out, indent);
  325.       }
  326.  
  327.    }
  328.  
  329.    public void uncaughtException(Thread t, Throwable e) {
  330.       if (this.parent != null) {
  331.          this.parent.uncaughtException(t, e);
  332.       } else {
  333.          if (!(e instanceof ThreadDeath)) {
  334.             e.printStackTrace(System.err);
  335.          }
  336.  
  337.       }
  338.    }
  339.  
  340.    public String toString() {
  341.       return this.getClass().getName() + "[name=" + this.getName() + ",maxpri=" + this.maxPriority + "]";
  342.    }
  343. }
  344.