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     public final void stop() {
 632         if (stopOrSuspend(false))
 633             Thread.currentThread().stop();
 634     }
 635 
 636     /**
 637      * Interrupts all threads in this thread group.
 638      * <p>
 639      * First, the {@code checkAccess} method of this thread group is
 640      * called with no arguments; this may result in a security exception.
 641      * <p>
 642      * This method then calls the {@code interrupt} method on all the
 643      * threads in this thread group and in all of its subgroups.
 644      *
 645      * @throws     SecurityException  if the current thread is not allowed
 646      *               to access this thread group or any of the threads in
 647      *               the thread group.
 648      * @see        java.lang.Thread#interrupt()
 649      * @see        java.lang.SecurityException
 650      * @see        java.lang.ThreadGroup#checkAccess()
 651      * @since      1.2
 652      */
 653     public final void interrupt() {
 654         int ngroupsSnapshot;
 655         ThreadGroup[] groupsSnapshot;
 656         synchronized (this) {
 657             checkAccess();
 658             for (int i = 0 ; i < nthreads ; i++) {
 659                 threads[i].interrupt();
 660             }
 661             ngroupsSnapshot = ngroups;
 662             if (groups != null) {
 663                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 664             } else {
 665                 groupsSnapshot = null;
 666             }
 667         }
 668         for (int i = 0 ; i < ngroupsSnapshot ; i++) {
 669             groupsSnapshot[i].interrupt();
 670         }
 671     }
 672 
 673     /**
 674      * Suspends all threads in this thread group.
 675      * <p>
 676      * First, the {@code checkAccess} method of this thread group is
 677      * called with no arguments; this may result in a security exception.
 678      * <p>
 679      * This method then calls the {@code suspend} method on all the
 680      * threads in this thread group and in all of its subgroups.
 681      *
 682      * @throws     SecurityException  if the current thread is not allowed
 683      *               to access this thread group or any of the threads in
 684      *               the thread group.
 685      * @see        java.lang.Thread#suspend()
 686      * @see        java.lang.SecurityException
 687      * @see        java.lang.ThreadGroup#checkAccess()
 688      * @since      1.0
 689      * @deprecated    This method is inherently deadlock-prone.  See
 690      *     {@link Thread#suspend} for details.
 691      */
 692     @Deprecated(since="1.2", forRemoval=true)
 693     @SuppressWarnings("removal")
 694     public final void suspend() {
 695         if (stopOrSuspend(true))
 696             Thread.currentThread().suspend();
 697     }
 698 
 699     /**
 700      * Helper method: recursively stops or suspends (as directed by the
 701      * boolean argument) all of the threads in this thread group and its
 702      * subgroups, except the current thread.  This method returns true
 703      * if (and only if) the current thread is found to be in this thread
 704      * group or one of its subgroups.
 705      */
 706     @SuppressWarnings({"deprecation", "removal"})
 707     private boolean stopOrSuspend(boolean suspend) {
 708         boolean suicide = false;
 709         Thread us = Thread.currentThread();
 710         int ngroupsSnapshot;
 711         ThreadGroup[] groupsSnapshot = null;
 712         synchronized (this) {
 713             checkAccess();
 714             for (int i = 0 ; i < nthreads ; i++) {
 715                 if (threads[i]==us)
 716                     suicide = true;
 717                 else if (suspend)
 718                     threads[i].suspend();
 719                 else
 720                     threads[i].stop();
 721             }
 722 
 723             ngroupsSnapshot = ngroups;
 724             if (groups != null) {
 725                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 726             }
 727         }
 728         for (int i = 0 ; i < ngroupsSnapshot ; i++)
 729             suicide = groupsSnapshot[i].stopOrSuspend(suspend) || suicide;
 730 
 731         return suicide;
 732     }
 733 
 734     /**
 735      * Resumes all threads in this thread group.
 736      * <p>
 737      * First, the {@code checkAccess} method of this thread group is
 738      * called with no arguments; this may result in a security exception.
 739      * <p>
 740      * This method then calls the {@code resume} method on all the
 741      * threads in this thread group and in all of its sub groups.
 742      *
 743      * @throws     SecurityException  if the current thread is not allowed to
 744      *               access this thread group or any of the threads in the
 745      *               thread group.
 746      * @see        java.lang.SecurityException
 747      * @see        java.lang.Thread#resume()
 748      * @see        java.lang.ThreadGroup#checkAccess()
 749      * @since      1.0
 750      * @deprecated    This method is used solely in conjunction with
 751      *       {@code Thread.suspend} and {@code ThreadGroup.suspend},
 752      *       both of which have been deprecated, as they are inherently
 753      *       deadlock-prone.  See {@link Thread#suspend} for details.
 754      */
 755     @Deprecated(since="1.2", forRemoval=true)
 756     @SuppressWarnings("removal")
 757     public final void resume() {
 758         int ngroupsSnapshot;
 759         ThreadGroup[] groupsSnapshot;
 760         synchronized (this) {
 761             checkAccess();
 762             for (int i = 0 ; i < nthreads ; i++) {
 763                 threads[i].resume();
 764             }
 765             ngroupsSnapshot = ngroups;
 766             if (groups != null) {
 767                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 768             } else {
 769                 groupsSnapshot = null;
 770             }
 771         }
 772         for (int i = 0 ; i < ngroupsSnapshot ; i++) {
 773             groupsSnapshot[i].resume();
 774         }
 775     }
 776 
 777     /**
 778      * Destroys this thread group and all of its subgroups. This thread
 779      * group must be empty, indicating that all threads that had been in
 780      * this thread group have since stopped.
 781      * <p>
 782      * First, the {@code checkAccess} method of this thread group is
 783      * called with no arguments; this may result in a security exception.
 784      *
 785      * @throws     IllegalThreadStateException  if the thread group is not
 786      *               empty or if the thread group has already been destroyed.
 787      * @throws     SecurityException  if the current thread cannot modify this
 788      *               thread group.
 789      * @see        java.lang.ThreadGroup#checkAccess()
 790      * @since      1.0
 791      *
 792      * @deprecated The API and mechanism for destroying a ThreadGroup is inherently
 793      *             flawed. The ability to explicitly or automatically destroy a
 794      *             thread group will be removed in a future release.
 795      */
 796     @Deprecated(since="16", forRemoval=true)
 797     public final void destroy() {
 798         int ngroupsSnapshot;
 799         ThreadGroup[] groupsSnapshot;
 800         synchronized (this) {
 801             checkAccess();
 802             if (destroyed || (nthreads > 0)) {
 803                 throw new IllegalThreadStateException();
 804             }
 805             ngroupsSnapshot = ngroups;
 806             if (groups != null) {
 807                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 808             } else {
 809                 groupsSnapshot = null;
 810             }
 811             if (parent != null) {
 812                 destroyed = true;
 813                 ngroups = 0;
 814                 groups = null;
 815                 nthreads = 0;
 816                 threads = null;
 817             }
 818         }
 819         for (int i = 0 ; i < ngroupsSnapshot ; i += 1) {
 820             groupsSnapshot[i].destroy();
 821         }
 822         if (parent != null) {
 823             parent.remove(this);
 824         }
 825     }
 826 
 827     /**
 828      * Adds the specified Thread group to this group.
 829      * @param g the specified Thread group to be added
 830      * @throws  IllegalThreadStateException If the Thread group has been destroyed.
 831      */
 832     private final void add(ThreadGroup g){
 833         synchronized (this) {
 834             if (destroyed) {
 835                 throw new IllegalThreadStateException();
 836             }
 837             if (groups == null) {
 838                 groups = new ThreadGroup[4];
 839             } else if (ngroups == groups.length) {
 840                 groups = Arrays.copyOf(groups, ngroups * 2);
 841             }
 842             groups[ngroups] = g;
 843 
 844             // This is done last so it doesn't matter in case the
 845             // thread is killed
 846             ngroups++;
 847         }
 848     }
 849 
 850     /**
 851      * Removes the specified Thread group from this group.
 852      * @param g the Thread group to be removed
 853      * @return if this Thread has already been destroyed.
 854      */
 855     private void remove(ThreadGroup g) {
 856         synchronized (this) {
 857             if (destroyed) {
 858                 return;
 859             }
 860             for (int i = 0 ; i < ngroups ; i++) {
 861                 if (groups[i] == g) {
 862                     ngroups -= 1;
 863                     System.arraycopy(groups, i + 1, groups, i, ngroups - i);
 864                     // Zap dangling reference to the dead group so that
 865                     // the garbage collector will collect it.
 866                     groups[ngroups] = null;
 867                     break;
 868                 }
 869             }
 870             if (nthreads == 0) {
 871                 notifyAll();
 872             }
 873             if (daemon && (nthreads == 0) &&
 874                 (nUnstartedThreads == 0) && (ngroups == 0))
 875             {
 876                 destroy();
 877             }
 878         }
 879     }
 880 
 881 
 882     /**
 883      * Increments the count of unstarted threads in the thread group.
 884      * Unstarted threads are not added to the thread group so that they
 885      * can be collected if they are never started, but they must be
 886      * counted so that daemon thread groups with unstarted threads in
 887      * them are not destroyed.
 888      */
 889     void addUnstarted() {
 890         synchronized(this) {
 891             if (destroyed) {
 892                 throw new IllegalThreadStateException();
 893             }
 894             nUnstartedThreads++;
 895         }
 896     }
 897 
 898     /**
 899      * Adds the specified thread to this thread group.
 900      *
 901      * <p> Note: This method is called from both library code
 902      * and the Virtual Machine. It is called from VM to add
 903      * certain system threads to the system thread group.
 904      *
 905      * @param  t
 906      *         the Thread to be added
 907      *
 908      * @throws IllegalThreadStateException
 909      *          if the Thread group has been destroyed
 910      */
 911     void add(Thread t) {
 912         synchronized (this) {
 913             if (destroyed) {
 914                 throw new IllegalThreadStateException();
 915             }
 916             if (threads == null) {
 917                 threads = new Thread[4];
 918             } else if (nthreads == threads.length) {
 919                 threads = Arrays.copyOf(threads, nthreads * 2);
 920             }
 921             threads[nthreads] = t;
 922 
 923             // This is done last so it doesn't matter in case the
 924             // thread is killed
 925             nthreads++;
 926 
 927             // The thread is now a fully fledged member of the group, even
 928             // though it may, or may not, have been started yet. It will prevent
 929             // the group from being destroyed so the unstarted Threads count is
 930             // decremented.
 931             nUnstartedThreads--;
 932         }
 933     }
 934 
 935     /**
 936      * Notifies the group that the thread {@code t} has failed
 937      * an attempt to start.
 938      *
 939      * <p> The state of this thread group is rolled back as if the
 940      * attempt to start the thread has never occurred. The thread is again
 941      * considered an unstarted member of the thread group, and a subsequent
 942      * attempt to start the thread is permitted.
 943      *
 944      * @param  t
 945      *         the Thread whose start method was invoked
 946      */
 947     void threadStartFailed(Thread t) {
 948         synchronized(this) {
 949             remove(t);
 950             nUnstartedThreads++;
 951         }
 952     }
 953 
 954     /**
 955      * Notifies the group that the thread {@code t} has terminated.
 956      *
 957      * <p> Destroy the group if all of the following conditions are
 958      * true: this is a daemon thread group; there are no more alive
 959      * or unstarted threads in the group; there are no subgroups in
 960      * this thread group.
 961      *
 962      * @param  t
 963      *         the Thread that has terminated
 964      */
 965     void threadTerminated(Thread t) {
 966         synchronized (this) {
 967             remove(t);
 968 
 969             if (nthreads == 0) {
 970                 notifyAll();
 971             }
 972             if (daemon && (nthreads == 0) &&
 973                 (nUnstartedThreads == 0) && (ngroups == 0))
 974             {
 975                 destroy();
 976             }
 977         }
 978     }
 979 
 980     /**
 981      * Removes the specified Thread from this group. Invoking this method
 982      * on a thread group that has been destroyed has no effect.
 983      *
 984      * @param  t
 985      *         the Thread to be removed
 986      */
 987     private void remove(Thread t) {
 988         synchronized (this) {
 989             if (destroyed) {
 990                 return;
 991             }
 992             for (int i = 0 ; i < nthreads ; i++) {
 993                 if (threads[i] == t) {
 994                     System.arraycopy(threads, i + 1, threads, i, --nthreads - i);
 995                     // Zap dangling reference to the dead thread so that
 996                     // the garbage collector will collect it.
 997                     threads[nthreads] = null;
 998                     break;
 999                 }
1000             }
1001         }
1002     }
1003 
1004     /**
1005      * Prints information about this thread group to the standard
1006      * output. This method is useful only for debugging.
1007      *
1008      * @since   1.0
1009      */
1010     public void list() {
1011         list(System.out, 0);
1012     }
1013     void list(PrintStream out, int indent) {
1014         int ngroupsSnapshot;
1015         ThreadGroup[] groupsSnapshot;
1016         synchronized (this) {
1017             for (int j = 0 ; j < indent ; j++) {
1018                 out.print(" ");
1019             }
1020             out.println(this);
1021             indent += 4;
1022             for (int i = 0 ; i < nthreads ; i++) {
1023                 for (int j = 0 ; j < indent ; j++) {
1024                     out.print(" ");
1025                 }
1026                 out.println(threads[i]);
1027             }
1028             ngroupsSnapshot = ngroups;
1029             if (groups != null) {
1030                 groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
1031             } else {
1032                 groupsSnapshot = null;
1033             }
1034         }
1035         for (int i = 0 ; i < ngroupsSnapshot ; i++) {
1036             groupsSnapshot[i].list(out, indent);
1037         }
1038     }
1039 
1040     /**
1041      * Called by the Java Virtual Machine when a thread in this
1042      * thread group stops because of an uncaught exception, and the thread
1043      * does not have a specific {@link Thread.UncaughtExceptionHandler}
1044      * installed.
1045      * <p>
1046      * The {@code uncaughtException} method of
1047      * {@code ThreadGroup} does the following:
1048      * <ul>
1049      * <li>If this thread group has a parent thread group, the
1050      *     {@code uncaughtException} method of that parent is called
1051      *     with the same two arguments.
1052      * <li>Otherwise, this method checks to see if there is a
1053      *     {@linkplain Thread#getDefaultUncaughtExceptionHandler default
1054      *     uncaught exception handler} installed, and if so, its
1055      *     {@code uncaughtException} method is called with the same
1056      *     two arguments.
1057      * <li>Otherwise, this method determines if the {@code Throwable}
1058      *     argument is an instance of {@link ThreadDeath}. If so, nothing
1059      *     special is done. Otherwise, a message containing the
1060      *     thread's name, as returned from the thread's {@link
1061      *     Thread#getName getName} method, and a stack backtrace,
1062      *     using the {@code Throwable}'s {@link
1063      *     Throwable#printStackTrace printStackTrace} method, is
1064      *     printed to the {@linkplain System#err standard error stream}.
1065      * </ul>
1066      * <p>
1067      * Applications can override this method in subclasses of
1068      * {@code ThreadGroup} to provide alternative handling of
1069      * uncaught exceptions.
1070      *
1071      * @param   t   the thread that is about to exit.
1072      * @param   e   the uncaught exception.
1073      * @since   1.0
1074      */
1075     public void uncaughtException(Thread t, Throwable e) {
1076         if (parent != null) {
1077             parent.uncaughtException(t, e);
1078         } else {
1079             Thread.UncaughtExceptionHandler ueh =
1080                 Thread.getDefaultUncaughtExceptionHandler();
1081             if (ueh != null) {
1082                 ueh.uncaughtException(t, e);
1083             } else if (!(e instanceof ThreadDeath)) {
1084                 System.err.print("Exception in thread \""
1085                                  + t.getName() + "\" ");
1086                 e.printStackTrace(System.err);
1087             }
1088         }
1089     }
1090 
1091     /**
1092      * Used by VM to control lowmem implicit suspension.
1093      *
1094      * @param b boolean to allow or disallow suspension
1095      * @return true on success
1096      * @since   1.1
1097      * @deprecated The definition of this call depends on {@link #suspend},
1098      *             which is deprecated.  Further, the behavior of this call
1099      *             was never specified.
1100      */
1101     @Deprecated(since="1.2", forRemoval=true)
1102     public boolean allowThreadSuspension(boolean b) {
1103         return true;
1104     }
1105 
1106     /**
1107      * Returns a string representation of this Thread group.
1108      *
1109      * @return  a string representation of this thread group.
1110      * @since   1.0
1111      */
1112     public String toString() {
1113         return getClass().getName() + "[name=" + getName() + ",maxpri=" + maxPriority + "]";
1114     }
1115 }