1 /*
   2  * Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.PrintStream;
  29 import java.util.Arrays;
  30 
  31 /**
  32  * A thread group represents a set of threads. In addition, a thread
  33  * group can also include other thread groups. The thread groups form
  34  * a tree in which every thread group except the initial thread group
  35  * has a parent.
  36  * <p>
  37  * A thread is allowed to access information about its own thread
  38  * group, but not to access information about its thread group's
  39  * parent thread group or any other thread groups.
  40  *
  41  * @since   1.0
  42  */
  43 /* The locking strategy for this code is to try to lock only one level of the
  44  * tree wherever possible, but otherwise to lock from the bottom up.
  45  * That is, from child thread groups to parents.
  46  * This has the advantage of limiting the number of locks that need to be held
  47  * and in particular avoids having to grab the lock for the root thread group,
  48  * (or a global lock) which would be a source of contention on a
  49  * multi-processor system with many thread groups.
  50  * This policy often leads to taking a snapshot of the state of a thread group
  51  * and working off of that snapshot, rather than holding the thread group locked
  52  * while we work on the children.
  53  */
  54 public class ThreadGroup implements Thread.UncaughtExceptionHandler {
  55     private final ThreadGroup parent;
  56     String name;
  57     int maxPriority;
  58     boolean destroyed;
  59     boolean daemon;
  60 
  61     int nUnstartedThreads = 0;
  62     int nthreads;
  63     Thread threads[];
  64 
  65     int ngroups;
  66     ThreadGroup groups[];
  67 
  68     /**
  69      * Creates an empty Thread group that is not in any Thread group.
  70      * This method is used to create the system Thread group.
  71      */
  72     private ThreadGroup() {     // called from C code
  73         this.name = "system";
  74         this.maxPriority = Thread.MAX_PRIORITY;
  75         this.parent = null;
  76     }
  77 
  78     /**
  79      * Constructs a new thread group. The parent of this new group is
  80      * the thread group of the currently running thread.
  81      * <p>
  82      * The {@code checkAccess} method of the parent thread group is
  83      * called with no arguments; this may result in a security exception.
  84      *
  85      * @param   name   the name of the new thread group.
  86      * @throws  SecurityException  if the current thread cannot create a
  87      *               thread in the specified thread group.
  88      * @see     java.lang.ThreadGroup#checkAccess()
  89      * @since   1.0
  90      */
  91     public ThreadGroup(String name) {
  92         this(Thread.currentThread().getThreadGroup(), name);
  93     }
  94 
  95     /**
  96      * Creates a new thread group. The parent of this new group is the
  97      * specified thread group.
  98      * <p>
  99      * The {@code checkAccess} method of the parent thread group is
 100      * called with no arguments; this may result in a security exception.
 101      *
 102      * @param     parent   the parent thread group.
 103      * @param     name     the name of the new thread group.
 104      * @throws    NullPointerException  if the thread group argument is
 105      *               {@code null}.
 106      * @throws    SecurityException  if the current thread cannot create a
 107      *               thread in the specified thread group.
 108      * @see     java.lang.SecurityException
 109      * @see     java.lang.ThreadGroup#checkAccess()
 110      * @since   1.0
 111      */
 112     public ThreadGroup(ThreadGroup parent, String name) {
 113         this(checkParentAccess(parent), parent, name);
 114     }
 115 
 116     private ThreadGroup(Void unused, ThreadGroup parent, String name) {
 117         this.name = name;
 118         this.maxPriority = parent.maxPriority;
 119         this.daemon = parent.daemon;
 120         this.parent = parent;
 121         parent.add(this);
 122     }
 123 
 124     /*
 125      * @throws  NullPointerException  if the parent argument is {@code null}
 126      * @throws  SecurityException     if the current thread cannot create a
 127      *                                thread in the specified thread group.
 128      */
 129     private static Void checkParentAccess(ThreadGroup parent) {
 130         parent.checkAccess();
 131         return null;
 132     }
 133 
 134     /**
 135      * Returns the name of this thread group.
 136      *
 137      * @return  the name of this thread group.
 138      * @since   1.0
 139      */
 140     public final String getName() {
 141         return name;
 142     }
 143 
 144     /**
 145      * Returns the parent of this thread group.
 146      * <p>
 147      * First, if the parent is not {@code null}, the
 148      * {@code checkAccess} method of the parent thread group is
 149      * called with no arguments; this may result in a security exception.
 150      *
 151      * @return  the parent of this thread group. The top-level thread group
 152      *          is the only thread group whose parent is {@code null}.
 153      * @throws  SecurityException  if the current thread cannot modify
 154      *               this thread group.
 155      * @see        java.lang.ThreadGroup#checkAccess()
 156      * @see        java.lang.SecurityException
 157      * @see        java.lang.RuntimePermission
 158      * @since   1.0
 159      */
 160     public final ThreadGroup getParent() {
 161         if (parent != null)
 162             parent.checkAccess();
 163         return parent;
 164     }
 165 
 166     /**
 167      * Returns the maximum priority of this thread group. Threads that are
 168      * part of this group cannot have a higher priority than the maximum
 169      * priority.
 170      *
 171      * @return  the maximum priority that a thread in this thread group
 172      *          can have.
 173      * @see     #setMaxPriority
 174      * @since   1.0
 175      */
 176     public final int getMaxPriority() {
 177         return maxPriority;
 178     }
 179 
 180     /**
 181      * Tests if this thread group is a daemon thread group. A
 182      * daemon thread group is automatically destroyed when its last
 183      * thread is stopped or its last thread group is destroyed.
 184      *
 185      * @return  {@code true} if this thread group is a daemon thread group;
 186      *          {@code false} otherwise.
 187      * @since   1.0
 188      *
 189      * @deprecated The API and mechanism for destroying a ThreadGroup is inherently
 190      *             flawed. The ability to explicitly or automatically destroy a
 191      *             thread group, and the concept of daemon thread group, will be
 192      *             removed in a future release.
 193      */
 194     @Deprecated(since="16", forRemoval=true)
 195     public final boolean isDaemon() {
 196         return daemon;
 197     }
 198 
 199     /**
 200      * Tests if this thread group has been destroyed.
 201      *
 202      * @return  true if this object is destroyed
 203      * @since   1.1
 204      *
 205      * @deprecated The API and mechanism for destroying a ThreadGroup is inherently
 206      *             flawed. The ability to explicitly or automatically destroy a
 207      *             thread group will be removed in a future release.
 208      */
 209     @Deprecated(since="16", forRemoval=true)
 210     public synchronized boolean isDestroyed() {
 211         return destroyed;
 212     }
 213 
 214     /**
 215      * Changes the daemon status of this thread group.
 216      * <p>
 217      * First, the {@code checkAccess} method of this thread group is
 218      * called with no arguments; this may result in a security exception.
 219      * <p>
 220      * A daemon thread group is automatically destroyed when its last
 221      * thread is stopped or its last thread group is destroyed.
 222      *
 223      * @param      daemon   if {@code true}, marks this thread group as
 224      *                      a daemon thread group; otherwise, marks this
 225      *                      thread group as normal.
 226      * @throws     SecurityException  if the current thread cannot modify
 227      *               this thread group.
 228      * @see        java.lang.SecurityException
 229      * @see        java.lang.ThreadGroup#checkAccess()
 230      * @since      1.0
 231      *
 232      * @deprecated The API and mechanism for destroying a ThreadGroup is inherently
 233      *             flawed. The ability to explicitly or automatically destroy a
 234      *             thread group, and the concept of daemon thread group, will be
 235      *             removed in a future release.
 236      */
 237     @Deprecated(since="16", forRemoval=true)
 238     public final void setDaemon(boolean daemon) {
 239         checkAccess();
 240         this.daemon = daemon;
 241     }
 242 
 243     /**
 244      * Sets the maximum priority of the group. Threads in the thread
 245      * group that already have a higher priority are not affected.
 246      * <p>
 247      * First, the {@code checkAccess} method of this thread group is
 248      * called with no arguments; this may result in a security exception.
 249      * <p>
 250      * If the {@code pri} argument is less than
 251      * {@link Thread#MIN_PRIORITY} or greater than
 252      * {@link Thread#MAX_PRIORITY}, the maximum priority of the group
 253      * remains unchanged.
 254      * <p>
 255      * Otherwise, the priority of this ThreadGroup object is set to the
 256      * smaller of the specified {@code pri} and the maximum permitted
 257      * priority of the parent of this thread group. (If this thread group
 258      * is the system thread group, which has no parent, then its maximum
 259      * priority is simply set to {@code pri}.) Then this method is
 260      * called recursively, with {@code pri} as its argument, for
 261      * every thread group that belongs to this thread group.
 262      *
 263      * @param      pri   the new priority of the thread group.
 264      * @throws     SecurityException  if the current thread cannot modify
 265      *               this thread group.
 266      * @see        #getMaxPriority
 267      * @see        java.lang.SecurityException
 268      * @see        java.lang.ThreadGroup#checkAccess()
 269      * @since      1.0
 270      */
 271     public final void setMaxPriority(int pri) {
 272         int ngroupsSnapshot;
 273         ThreadGroup[] groupsSnapshot;
 274         synchronized (this) {
 275             checkAccess();
 276             if (pri < Thread.MIN_PRIORITY || pri > Thread.MAX_PRIORITY) {
 277                 return;
 278             }
 279             maxPriority = (parent != null) ? Math.min(pri, parent.maxPriority) : pri;
 280             ngroupsSnapshot = ngroups;
 281             if (groups != null) {
 282                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 283             } else {
 284                 groupsSnapshot = null;
 285             }
 286         }
 287         for (int i = 0 ; i < ngroupsSnapshot ; i++) {
 288             groupsSnapshot[i].setMaxPriority(pri);
 289         }
 290     }
 291 
 292     /**
 293      * Tests if this thread group is either the thread group
 294      * argument or one of its ancestor thread groups.
 295      *
 296      * @param   g   a thread group.
 297      * @return  {@code true} if this thread group is the thread group
 298      *          argument or one of its ancestor thread groups;
 299      *          {@code false} otherwise.
 300      * @since   1.0
 301      */
 302     public final boolean parentOf(ThreadGroup g) {
 303         for (; g != null ; g = g.parent) {
 304             if (g == this) {
 305                 return true;
 306             }
 307         }
 308         return false;
 309     }
 310 
 311     /**
 312      * Determines if the currently running thread has permission to
 313      * modify this thread group.
 314      * <p>
 315      * If there is a security manager, its {@code checkAccess} method
 316      * is called with this thread group as its argument. This may result
 317      * in throwing a {@code SecurityException}.
 318      *
 319      * @throws     SecurityException  if the current thread is not allowed to
 320      *               access this thread group.
 321      * @see        java.lang.SecurityManager#checkAccess(java.lang.ThreadGroup)
 322      * @since      1.0
 323      * @deprecated This method is only useful in conjunction with
 324      *       {@linkplain SecurityManager the Security Manager}, which is
 325      *       deprecated and subject to removal in a future release.
 326      *       Consequently, this method is also deprecated and subject to
 327      *       removal. There is no replacement for the Security Manager or this
 328      *       method.
 329      */
 330     @Deprecated(since="17", forRemoval=true)
 331     public final void checkAccess() {
 332         @SuppressWarnings("removal")
 333         SecurityManager security = System.getSecurityManager();
 334         if (security != null) {
 335             security.checkAccess(this);
 336         }
 337     }
 338 
 339     /**
 340      * Returns an estimate of the number of active threads in this thread
 341      * group and its subgroups. Recursively iterates over all subgroups in
 342      * this thread group.
 343      *
 344      * <p> The value returned is only an estimate because the number of
 345      * threads may change dynamically while this method traverses internal
 346      * data structures, and might be affected by the presence of certain
 347      * system threads. This method is intended primarily for debugging
 348      * and monitoring purposes.
 349      *
 350      * @return  an estimate of the number of active threads in this thread
 351      *          group and in any other thread group that has this thread
 352      *          group as an ancestor
 353      *
 354      * @since   1.0
 355      */
 356     public int activeCount() {
 357         int result;
 358         // Snapshot sub-group data so we don't hold this lock
 359         // while our children are computing.
 360         int ngroupsSnapshot;
 361         ThreadGroup[] groupsSnapshot;
 362         synchronized (this) {
 363             if (destroyed) {
 364                 return 0;
 365             }
 366             result = nthreads;
 367             ngroupsSnapshot = ngroups;
 368             if (groups != null) {
 369                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 370             } else {
 371                 groupsSnapshot = null;
 372             }
 373         }
 374         for (int i = 0 ; i < ngroupsSnapshot ; i++) {
 375             result += groupsSnapshot[i].activeCount();
 376         }
 377         return result;
 378     }
 379 
 380     /**
 381      * Copies into the specified array every active thread in this
 382      * thread group and its subgroups.
 383      *
 384      * <p> An invocation of this method behaves in exactly the same
 385      * way as the invocation
 386      *
 387      * <blockquote>
 388      * {@linkplain #enumerate(Thread[], boolean) enumerate}{@code (list, true)}
 389      * </blockquote>
 390      *
 391      * @param  list
 392      *         an array into which to put the list of threads
 393      *
 394      * @return  the number of threads put into the array
 395      *
 396      * @throws  SecurityException
 397      *          if {@linkplain #checkAccess checkAccess} determines that
 398      *          the current thread cannot access this thread group
 399      *
 400      * @since   1.0
 401      */
 402     public int enumerate(Thread[] list) {
 403         checkAccess();
 404         return enumerate(list, 0, true);
 405     }
 406 
 407     /**
 408      * Copies into the specified array every active thread in this
 409      * thread group. If {@code recurse} is {@code true},
 410      * this method recursively enumerates all subgroups of this
 411      * thread group and references to every active thread in these
 412      * subgroups are also included. If the array is too short to
 413      * hold all the threads, the extra threads are silently ignored.
 414      *
 415      * <p> An application might use the {@linkplain #activeCount activeCount}
 416      * method to get an estimate of how big the array should be, however
 417      * <i>if the array is too short to hold all the threads, the extra threads
 418      * are silently ignored.</i>  If it is critical to obtain every active
 419      * thread in this thread group, the caller should verify that the returned
 420      * int value is strictly less than the length of {@code list}.
 421      *
 422      * <p> Due to the inherent race condition in this method, it is recommended
 423      * that the method only be used for debugging and monitoring purposes.
 424      *
 425      * @param  list
 426      *         an array into which to put the list of threads
 427      *
 428      * @param  recurse
 429      *         if {@code true}, recursively enumerate all subgroups of this
 430      *         thread group
 431      *
 432      * @return  the number of threads put into the array
 433      *
 434      * @throws  SecurityException
 435      *          if {@linkplain #checkAccess checkAccess} determines that
 436      *          the current thread cannot access this thread group
 437      *
 438      * @since   1.0
 439      */
 440     public int enumerate(Thread[] list, boolean recurse) {
 441         checkAccess();
 442         return enumerate(list, 0, recurse);
 443     }
 444 
 445     private int enumerate(Thread[] list, int n, boolean recurse) {
 446         int ngroupsSnapshot = 0;
 447         ThreadGroup[] groupsSnapshot = null;
 448         synchronized (this) {
 449             if (destroyed) {
 450                 return n;
 451             }
 452             int nt = nthreads;
 453             if (nt > list.length - n) {
 454                 nt = list.length - n;
 455             }
 456             for (int i = 0; i < nt; i++) {
 457                 if (threads[i].isAlive()) {
 458                     list[n++] = threads[i];
 459                 }
 460             }
 461             if (recurse) {
 462                 ngroupsSnapshot = ngroups;
 463                 if (groups != null) {
 464                     groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 465                 } else {
 466                     groupsSnapshot = null;
 467                 }
 468             }
 469         }
 470         if (recurse) {
 471             for (int i = 0 ; i < ngroupsSnapshot ; i++) {
 472                 n = groupsSnapshot[i].enumerate(list, n, true);
 473             }
 474         }
 475         return n;
 476     }
 477 
 478     /**
 479      * Returns an estimate of the number of active groups in this
 480      * thread group and its subgroups. Recursively iterates over
 481      * all subgroups in this thread group.
 482      *
 483      * <p> The value returned is only an estimate because the number of
 484      * thread groups may change dynamically while this method traverses
 485      * internal data structures. This method is intended primarily for
 486      * debugging and monitoring purposes.
 487      *
 488      * @return  the number of active thread groups with this thread group as
 489      *          an ancestor
 490      *
 491      * @since   1.0
 492      */
 493     public int activeGroupCount() {
 494         int ngroupsSnapshot;
 495         ThreadGroup[] groupsSnapshot;
 496         synchronized (this) {
 497             if (destroyed) {
 498                 return 0;
 499             }
 500             ngroupsSnapshot = ngroups;
 501             if (groups != null) {
 502                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 503             } else {
 504                 groupsSnapshot = null;
 505             }
 506         }
 507         int n = ngroupsSnapshot;
 508         for (int i = 0 ; i < ngroupsSnapshot ; i++) {
 509             n += groupsSnapshot[i].activeGroupCount();
 510         }
 511         return n;
 512     }
 513 
 514     /**
 515      * Copies into the specified array references to every active
 516      * subgroup in this thread group and its subgroups.
 517      *
 518      * <p> An invocation of this method behaves in exactly the same
 519      * way as the invocation
 520      *
 521      * <blockquote>
 522      * {@linkplain #enumerate(ThreadGroup[], boolean) enumerate}{@code (list, true)}
 523      * </blockquote>
 524      *
 525      * @param  list
 526      *         an array into which to put the list of thread groups
 527      *
 528      * @return  the number of thread groups put into the array
 529      *
 530      * @throws  SecurityException
 531      *          if {@linkplain #checkAccess checkAccess} determines that
 532      *          the current thread cannot access this thread group
 533      *
 534      * @since   1.0
 535      */
 536     public int enumerate(ThreadGroup[] list) {
 537         checkAccess();
 538         return enumerate(list, 0, true);
 539     }
 540 
 541     /**
 542      * Copies into the specified array references to every active
 543      * subgroup in this thread group. If {@code recurse} is
 544      * {@code true}, this method recursively enumerates all subgroups of this
 545      * thread group and references to every active thread group in these
 546      * subgroups are also included.
 547      *
 548      * <p> An application might use the
 549      * {@linkplain #activeGroupCount activeGroupCount} method to
 550      * get an estimate of how big the array should be, however <i>if the
 551      * array is too short to hold all the thread groups, the extra thread
 552      * groups are silently ignored.</i>  If it is critical to obtain every
 553      * active subgroup in this thread group, the caller should verify that
 554      * the returned int value is strictly less than the length of
 555      * {@code list}.
 556      *
 557      * <p> Due to the inherent race condition in this method, it is recommended
 558      * that the method only be used for debugging and monitoring purposes.
 559      *
 560      * @param  list
 561      *         an array into which to put the list of thread groups
 562      *
 563      * @param  recurse
 564      *         if {@code true}, recursively enumerate all subgroups
 565      *
 566      * @return  the number of thread groups put into the array
 567      *
 568      * @throws  SecurityException
 569      *          if {@linkplain #checkAccess checkAccess} determines that
 570      *          the current thread cannot access this thread group
 571      *
 572      * @since   1.0
 573      */
 574     public int enumerate(ThreadGroup[] list, boolean recurse) {
 575         checkAccess();
 576         return enumerate(list, 0, recurse);
 577     }
 578 
 579     private int enumerate(ThreadGroup[] list, int n, boolean recurse) {
 580         int ngroupsSnapshot = 0;
 581         ThreadGroup[] groupsSnapshot = null;
 582         synchronized (this) {
 583             if (destroyed) {
 584                 return n;
 585             }
 586             int ng = ngroups;
 587             if (ng > list.length - n) {
 588                 ng = list.length - n;
 589             }
 590             if (ng > 0) {
 591                 System.arraycopy(groups, 0, list, n, ng);
 592                 n += ng;
 593             }
 594             if (recurse) {
 595                 ngroupsSnapshot = ngroups;
 596                 if (groups != null) {
 597                     groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 598                 } else {
 599                     groupsSnapshot = null;
 600                 }
 601             }
 602         }
 603         if (recurse) {
 604             for (int i = 0 ; i < ngroupsSnapshot ; i++) {
 605                 n = groupsSnapshot[i].enumerate(list, n, true);
 606             }
 607         }
 608         return n;
 609     }
 610 
 611     /**
 612      * Stops all threads in this thread group.
 613      * <p>
 614      * First, the {@code checkAccess} method of this thread group is
 615      * called with no arguments; this may result in a security exception.
 616      * <p>
 617      * This method then calls the {@code stop} method on all the
 618      * threads in this thread group and in all of its subgroups.
 619      *
 620      * @throws     SecurityException  if the current thread is not allowed
 621      *               to access this thread group or any of the threads in
 622      *               the thread group.
 623      * @see        java.lang.SecurityException
 624      * @see        java.lang.Thread#stop()
 625      * @see        java.lang.ThreadGroup#checkAccess()
 626      * @since      1.0
 627      * @deprecated    This method is inherently unsafe.  See
 628      *     {@link Thread#stop} for details.
 629      */
 630     @Deprecated(since="1.2", forRemoval=true)
 631     @SuppressWarnings("removal")
 632     public final void stop() {
 633         if (stopOrSuspend(false))
 634             Thread.currentThread().stop();
 635     }
 636 
 637     /**
 638      * Interrupts all threads in this thread group.
 639      * <p>
 640      * First, the {@code checkAccess} method of this thread group is
 641      * called with no arguments; this may result in a security exception.
 642      * <p>
 643      * This method then calls the {@code interrupt} method on all the
 644      * threads in this thread group and in all of its subgroups.
 645      *
 646      * @throws     SecurityException  if the current thread is not allowed
 647      *               to access this thread group or any of the threads in
 648      *               the thread group.
 649      * @see        java.lang.Thread#interrupt()
 650      * @see        java.lang.SecurityException
 651      * @see        java.lang.ThreadGroup#checkAccess()
 652      * @since      1.2
 653      */
 654     public final void interrupt() {
 655         int ngroupsSnapshot;
 656         ThreadGroup[] groupsSnapshot;
 657         synchronized (this) {
 658             checkAccess();
 659             for (int i = 0 ; i < nthreads ; i++) {
 660                 threads[i].interrupt();
 661             }
 662             ngroupsSnapshot = ngroups;
 663             if (groups != null) {
 664                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 665             } else {
 666                 groupsSnapshot = null;
 667             }
 668         }
 669         for (int i = 0 ; i < ngroupsSnapshot ; i++) {
 670             groupsSnapshot[i].interrupt();
 671         }
 672     }
 673 
 674     /**
 675      * Suspends all threads in this thread group.
 676      * <p>
 677      * First, the {@code checkAccess} method of this thread group is
 678      * called with no arguments; this may result in a security exception.
 679      * <p>
 680      * This method then calls the {@code suspend} method on all the
 681      * threads in this thread group and in all of its subgroups.
 682      *
 683      * @throws     SecurityException  if the current thread is not allowed
 684      *               to access this thread group or any of the threads in
 685      *               the thread group.
 686      * @see        java.lang.Thread#suspend()
 687      * @see        java.lang.SecurityException
 688      * @see        java.lang.ThreadGroup#checkAccess()
 689      * @since      1.0
 690      * @deprecated    This method is inherently deadlock-prone.  See
 691      *     {@link Thread#suspend} for details.
 692      */
 693     @Deprecated(since="1.2", forRemoval=true)
 694     @SuppressWarnings("removal")
 695     public final void suspend() {
 696         if (stopOrSuspend(true))
 697             Thread.currentThread().suspend();
 698     }
 699 
 700     /**
 701      * Helper method: recursively stops or suspends (as directed by the
 702      * boolean argument) all of the threads in this thread group and its
 703      * subgroups, except the current thread.  This method returns true
 704      * if (and only if) the current thread is found to be in this thread
 705      * group or one of its subgroups.
 706      */
 707     @SuppressWarnings({"deprecation", "removal"})
 708     private boolean stopOrSuspend(boolean suspend) {
 709         boolean suicide = false;
 710         Thread us = Thread.currentThread();
 711         int ngroupsSnapshot;
 712         ThreadGroup[] groupsSnapshot = null;
 713         synchronized (this) {
 714             checkAccess();
 715             for (int i = 0 ; i < nthreads ; i++) {
 716                 if (threads[i]==us)
 717                     suicide = true;
 718                 else if (suspend)
 719                     threads[i].suspend();
 720                 else
 721                     threads[i].stop();
 722             }
 723 
 724             ngroupsSnapshot = ngroups;
 725             if (groups != null) {
 726                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 727             }
 728         }
 729         for (int i = 0 ; i < ngroupsSnapshot ; i++)
 730             suicide = groupsSnapshot[i].stopOrSuspend(suspend) || suicide;
 731 
 732         return suicide;
 733     }
 734 
 735     /**
 736      * Resumes all threads in this thread group.
 737      * <p>
 738      * First, the {@code checkAccess} method of this thread group is
 739      * called with no arguments; this may result in a security exception.
 740      * <p>
 741      * This method then calls the {@code resume} method on all the
 742      * threads in this thread group and in all of its sub groups.
 743      *
 744      * @throws     SecurityException  if the current thread is not allowed to
 745      *               access this thread group or any of the threads in the
 746      *               thread group.
 747      * @see        java.lang.SecurityException
 748      * @see        java.lang.Thread#resume()
 749      * @see        java.lang.ThreadGroup#checkAccess()
 750      * @since      1.0
 751      * @deprecated    This method is used solely in conjunction with
 752      *       {@code Thread.suspend} and {@code ThreadGroup.suspend},
 753      *       both of which have been deprecated, as they are inherently
 754      *       deadlock-prone.  See {@link Thread#suspend} for details.
 755      */
 756     @Deprecated(since="1.2", forRemoval=true)
 757     @SuppressWarnings("removal")
 758     public final void resume() {
 759         int ngroupsSnapshot;
 760         ThreadGroup[] groupsSnapshot;
 761         synchronized (this) {
 762             checkAccess();
 763             for (int i = 0 ; i < nthreads ; i++) {
 764                 threads[i].resume();
 765             }
 766             ngroupsSnapshot = ngroups;
 767             if (groups != null) {
 768                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 769             } else {
 770                 groupsSnapshot = null;
 771             }
 772         }
 773         for (int i = 0 ; i < ngroupsSnapshot ; i++) {
 774             groupsSnapshot[i].resume();
 775         }
 776     }
 777 
 778     /**
 779      * Destroys this thread group and all of its subgroups. This thread
 780      * group must be empty, indicating that all threads that had been in
 781      * this thread group have since stopped.
 782      * <p>
 783      * First, the {@code checkAccess} method of this thread group is
 784      * called with no arguments; this may result in a security exception.
 785      *
 786      * @throws     IllegalThreadStateException  if the thread group is not
 787      *               empty or if the thread group has already been destroyed.
 788      * @throws     SecurityException  if the current thread cannot modify this
 789      *               thread group.
 790      * @see        java.lang.ThreadGroup#checkAccess()
 791      * @since      1.0
 792      *
 793      * @deprecated The API and mechanism for destroying a ThreadGroup is inherently
 794      *             flawed. The ability to explicitly or automatically destroy a
 795      *             thread group will be removed in a future release.
 796      */
 797     @Deprecated(since="16", forRemoval=true)
 798     public final void destroy() {
 799         int ngroupsSnapshot;
 800         ThreadGroup[] groupsSnapshot;
 801         synchronized (this) {
 802             checkAccess();
 803             if (destroyed || (nthreads > 0)) {
 804                 throw new IllegalThreadStateException();
 805             }
 806             ngroupsSnapshot = ngroups;
 807             if (groups != null) {
 808                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 809             } else {
 810                 groupsSnapshot = null;
 811             }
 812             if (parent != null) {
 813                 destroyed = true;
 814                 ngroups = 0;
 815                 groups = null;
 816                 nthreads = 0;
 817                 threads = null;
 818             }
 819         }
 820         for (int i = 0 ; i < ngroupsSnapshot ; i += 1) {
 821             groupsSnapshot[i].destroy();
 822         }
 823         if (parent != null) {
 824             parent.remove(this);
 825         }
 826     }
 827 
 828     /**
 829      * Adds the specified Thread group to this group.
 830      * @param g the specified Thread group to be added
 831      * @throws  IllegalThreadStateException If the Thread group has been destroyed.
 832      */
 833     private final void add(ThreadGroup g){
 834         synchronized (this) {
 835             if (destroyed) {
 836                 throw new IllegalThreadStateException();
 837             }
 838             if (groups == null) {
 839                 groups = new ThreadGroup[4];
 840             } else if (ngroups == groups.length) {
 841                 groups = Arrays.copyOf(groups, ngroups * 2);
 842             }
 843             groups[ngroups] = g;
 844 
 845             // This is done last so it doesn't matter in case the
 846             // thread is killed
 847             ngroups++;
 848         }
 849     }
 850 
 851     /**
 852      * Removes the specified Thread group from this group.
 853      * @param g the Thread group to be removed
 854      * @return if this Thread has already been destroyed.
 855      */
 856     private void remove(ThreadGroup g) {
 857         synchronized (this) {
 858             if (destroyed) {
 859                 return;
 860             }
 861             for (int i = 0 ; i < ngroups ; i++) {
 862                 if (groups[i] == g) {
 863                     ngroups -= 1;
 864                     System.arraycopy(groups, i + 1, groups, i, ngroups - i);
 865                     // Zap dangling reference to the dead group so that
 866                     // the garbage collector will collect it.
 867                     groups[ngroups] = null;
 868                     break;
 869                 }
 870             }
 871             if (nthreads == 0) {
 872                 notifyAll();
 873             }
 874             if (daemon && (nthreads == 0) &&
 875                 (nUnstartedThreads == 0) && (ngroups == 0))
 876             {
 877                 destroy();
 878             }
 879         }
 880     }
 881 
 882 
 883     /**
 884      * Increments the count of unstarted threads in the thread group.
 885      * Unstarted threads are not added to the thread group so that they
 886      * can be collected if they are never started, but they must be
 887      * counted so that daemon thread groups with unstarted threads in
 888      * them are not destroyed.
 889      */
 890     void addUnstarted() {
 891         synchronized(this) {
 892             if (destroyed) {
 893                 throw new IllegalThreadStateException();
 894             }
 895             nUnstartedThreads++;
 896         }
 897     }
 898 
 899     /**
 900      * Adds the specified thread to this thread group.
 901      *
 902      * <p> Note: This method is called from both library code
 903      * and the Virtual Machine. It is called from VM to add
 904      * certain system threads to the system thread group.
 905      *
 906      * @param  t
 907      *         the Thread to be added
 908      *
 909      * @throws IllegalThreadStateException
 910      *          if the Thread group has been destroyed
 911      */
 912     void add(Thread t) {
 913         synchronized (this) {
 914             if (destroyed) {
 915                 throw new IllegalThreadStateException();
 916             }
 917             if (threads == null) {
 918                 threads = new Thread[4];
 919             } else if (nthreads == threads.length) {
 920                 threads = Arrays.copyOf(threads, nthreads * 2);
 921             }
 922             threads[nthreads] = t;
 923 
 924             // This is done last so it doesn't matter in case the
 925             // thread is killed
 926             nthreads++;
 927 
 928             // The thread is now a fully fledged member of the group, even
 929             // though it may, or may not, have been started yet. It will prevent
 930             // the group from being destroyed so the unstarted Threads count is
 931             // decremented.
 932             nUnstartedThreads--;
 933         }
 934     }
 935 
 936     /**
 937      * Notifies the group that the thread {@code t} has failed
 938      * an attempt to start.
 939      *
 940      * <p> The state of this thread group is rolled back as if the
 941      * attempt to start the thread has never occurred. The thread is again
 942      * considered an unstarted member of the thread group, and a subsequent
 943      * attempt to start the thread is permitted.
 944      *
 945      * @param  t
 946      *         the Thread whose start method was invoked
 947      */
 948     void threadStartFailed(Thread t) {
 949         synchronized(this) {
 950             remove(t);
 951             nUnstartedThreads++;
 952         }
 953     }
 954 
 955     /**
 956      * Notifies the group that the thread {@code t} has terminated.
 957      *
 958      * <p> Destroy the group if all of the following conditions are
 959      * true: this is a daemon thread group; there are no more alive
 960      * or unstarted threads in the group; there are no subgroups in
 961      * this thread group.
 962      *
 963      * @param  t
 964      *         the Thread that has terminated
 965      */
 966     void threadTerminated(Thread t) {
 967         synchronized (this) {
 968             remove(t);
 969 
 970             if (nthreads == 0) {
 971                 notifyAll();
 972             }
 973             if (daemon && (nthreads == 0) &&
 974                 (nUnstartedThreads == 0) && (ngroups == 0))
 975             {
 976                 destroy();
 977             }
 978         }
 979     }
 980 
 981     /**
 982      * Removes the specified Thread from this group. Invoking this method
 983      * on a thread group that has been destroyed has no effect.
 984      *
 985      * @param  t
 986      *         the Thread to be removed
 987      */
 988     private void remove(Thread t) {
 989         synchronized (this) {
 990             if (destroyed) {
 991                 return;
 992             }
 993             for (int i = 0 ; i < nthreads ; i++) {
 994                 if (threads[i] == t) {
 995                     System.arraycopy(threads, i + 1, threads, i, --nthreads - i);
 996                     // Zap dangling reference to the dead thread so that
 997                     // the garbage collector will collect it.
 998                     threads[nthreads] = null;
 999                     break;
1000                 }
1001             }
1002         }
1003     }
1004 
1005     /**
1006      * Prints information about this thread group to the standard
1007      * output. This method is useful only for debugging.
1008      *
1009      * @since   1.0
1010      */
1011     public void list() {
1012         list(System.out, 0);
1013     }
1014     void list(PrintStream out, int indent) {
1015         int ngroupsSnapshot;
1016         ThreadGroup[] groupsSnapshot;
1017         synchronized (this) {
1018             for (int j = 0 ; j < indent ; j++) {
1019                 out.print(" ");
1020             }
1021             out.println(this);
1022             indent += 4;
1023             for (int i = 0 ; i < nthreads ; i++) {
1024                 for (int j = 0 ; j < indent ; j++) {
1025                     out.print(" ");
1026                 }
1027                 out.println(threads[i]);
1028             }
1029             ngroupsSnapshot = ngroups;
1030             if (groups != null) {
1031                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
1032             } else {
1033                 groupsSnapshot = null;
1034             }
1035         }
1036         for (int i = 0 ; i < ngroupsSnapshot ; i++) {
1037             groupsSnapshot[i].list(out, indent);
1038         }
1039     }
1040 
1041     /**
1042      * Called by the Java Virtual Machine when a thread in this
1043      * thread group stops because of an uncaught exception, and the thread
1044      * does not have a specific {@link Thread.UncaughtExceptionHandler}
1045      * installed.
1046      * <p>
1047      * The {@code uncaughtException} method of
1048      * {@code ThreadGroup} does the following:
1049      * <ul>
1050      * <li>If this thread group has a parent thread group, the
1051      *     {@code uncaughtException} method of that parent is called
1052      *     with the same two arguments.
1053      * <li>Otherwise, this method checks to see if there is a
1054      *     {@linkplain Thread#getDefaultUncaughtExceptionHandler default
1055      *     uncaught exception handler} installed, and if so, its
1056      *     {@code uncaughtException} method is called with the same
1057      *     two arguments.
1058      * <li>Otherwise, this method determines if the {@code Throwable}
1059      *     argument is an instance of {@link ThreadDeath}. If so, nothing
1060      *     special is done. Otherwise, a message containing the
1061      *     thread's name, as returned from the thread's {@link
1062      *     Thread#getName getName} method, and a stack backtrace,
1063      *     using the {@code Throwable}'s {@link
1064      *     Throwable#printStackTrace printStackTrace} method, is
1065      *     printed to the {@linkplain System#err standard error stream}.
1066      * </ul>
1067      * <p>
1068      * Applications can override this method in subclasses of
1069      * {@code ThreadGroup} to provide alternative handling of
1070      * uncaught exceptions.
1071      *
1072      * @param   t   the thread that is about to exit.
1073      * @param   e   the uncaught exception.
1074      * @since   1.0
1075      */
1076     public void uncaughtException(Thread t, Throwable e) {
1077         if (parent != null) {
1078             parent.uncaughtException(t, e);
1079         } else {
1080             Thread.UncaughtExceptionHandler ueh =
1081                 Thread.getDefaultUncaughtExceptionHandler();
1082             if (ueh != null) {
1083                 ueh.uncaughtException(t, e);
1084             } else if (!(e instanceof ThreadDeath)) {
1085                 System.err.print("Exception in thread \""
1086                                  + t.getName() + "\" ");
1087                 e.printStackTrace(System.err);
1088             }
1089         }
1090     }
1091 
1092     /**
1093      * Used by VM to control lowmem implicit suspension.
1094      *
1095      * @param b boolean to allow or disallow suspension
1096      * @return true on success
1097      * @since   1.1
1098      * @deprecated The definition of this call depends on {@link #suspend},
1099      *             which is deprecated.  Further, the behavior of this call
1100      *             was never specified.
1101      */
1102     @Deprecated(since="1.2", forRemoval=true)
1103     public boolean allowThreadSuspension(boolean b) {
1104         return true;
1105     }
1106 
1107     /**
1108      * Returns a string representation of this Thread group.
1109      *
1110      * @return  a string representation of this thread group.
1111      * @since   1.0
1112      */
1113     public String toString() {
1114         return getClass().getName() + "[name=" + getName() + ",maxpri=" + maxPriority + "]";
1115     }
1116 }