< prev index next >

src/java.base/share/classes/java/lang/ThreadGroup.java

Print this page

   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 }

   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.lang.ref.WeakReference;
  30 import java.util.ArrayList;
  31 import java.util.Arrays;
  32 import java.util.List;
  33 import java.util.Map;
  34 import java.util.Objects;
  35 import java.util.stream.Collectors;
  36 import java.util.stream.Stream;
  37 import jdk.internal.misc.VM;
  38 
  39 /**
  40  * A thread group represents a set of threads. In addition, a thread
  41  * group can also include other thread groups. The thread groups form
  42  * a tree in which every thread group except the initial thread group
  43  * has a parent.
  44  *
  45  * <p> A thread group has a name and maximum priority. The name is specified
  46  * when creating the group and cannot be changed. The group's maximum priority
  47  * is the maximum priority for threads created in the group. It is initially
  48  * inherited from the parent thread group but may be changed using the {@link
  49  * #setMaxPriority(int)} method.
  50  *
  51  * <p> A thread group is weakly <a href="ref/package-summary.html#reachability">
  52  * <em>reachable</em></a> from its parent group so that it is eligible for garbage
  53  * collection when there are no {@linkplain Thread#isAlive() live} threads in the
  54  * group and is otherwise <i>unreachable</i>.
  55  *
  56  * <p> Unless otherwise specified, passing a {@code null} argument to a constructor
  57  * or method in this class will cause a {@link NullPointerException} to be thrown.
  58  *
  59  * @apiNote
  60  * Thread groups provided a way in early JDK releases to group threads and provide
  61  * a form of <i>job control</i> for threads. Thread groups supported the isolation
  62  * of applets and defined methods intended for diagnostic purposes. The concept
  63  * of thread group is obsolete. It should be rare for new applications to create
  64  * thread groups or interact with this API.
  65  *
  66  * @since   1.0
  67  */











  68 public class ThreadGroup implements Thread.UncaughtExceptionHandler {
  69     /**
  70      * All fields are accessed directly by the VM and from JVMTI functions.
  71      * Operations that require synchronization on more than one group in the
  72      * tree should synchronize on the parent group before synchronizing on
  73      * the child group.
  74      */
  75     private final ThreadGroup parent;
  76     private final String name;
  77     private volatile int maxPriority;


  78 
  79     // strongly reachable from this group
  80     private int ngroups;
  81     private ThreadGroup[] groups;
  82 
  83     // weakly reachable from this group
  84     private int nweaks;
  85     private WeakReference<ThreadGroup>[] weaks;
  86 
  87     /**
  88      * Creates the top-level "system" ThreadGroup.
  89      *
  90      * @apiNote This method is invoked by the VM early startup.
  91      */
  92     private ThreadGroup() {
  93         this.parent = null;
  94         this.name = "system";
  95         this.maxPriority = Thread.MAX_PRIORITY;
  96     }
  97 
  98     /**
  99      * Creates a ThreadGroup without any permission or other checks.
 100      */
 101     ThreadGroup(ThreadGroup parent, String name, int maxPriority) {
 102         this.parent = parent;
 103         this.name = name;
 104         this.maxPriority = maxPriority;
 105         if (VM.isBooted()) {
 106             parent.synchronizedAddWeak(this);
 107         } else {
 108             // keep strong reference to the "main" and other groups created
 109             // early in the VM startup to avoid use weak references during
 110             // when starting the reference handlers.
 111             parent.synchronizedAddStrong(this);
 112         }
 113     }
 114 
 115     private ThreadGroup(Void unused, ThreadGroup parent, String name) {
 116         this(parent, name, parent.getMaxPriority());
 117     }
 118 
 119     private static Void checkParentAccess(ThreadGroup parent) {
 120         parent.checkAccess();
 121         return null;
 122     }
 123 
 124     /**
 125      * Constructs a new thread group. The parent of this new group is
 126      * the thread group of the currently running thread.
 127      * <p>
 128      * The {@code checkAccess} method of the parent thread group is
 129      * called with no arguments; this may result in a security exception.
 130      *
 131      * @param   name   the name of the new thread group, can be {@code null}
 132      * @throws  SecurityException  if the current thread cannot create a
 133      *               thread in the specified thread group.
 134      *
 135      * @deprecated
 136      * Thread groups provided a way in early JDK releases to group threads and
 137      * provide a form of <i>job control</i> for threads. Thread groups supported
 138      * the isolation of applets and defined methods intended for diagnostic
 139      * purposes. The concept of thread group is obsolete. It should be rare for
 140      * new applications to create thread groups.
 141      *
 142      * @see     java.lang.ThreadGroup#checkAccess()

 143      */
 144     @Deprecated(since = "99")
 145     public ThreadGroup(String name) {
 146         this(Thread.currentThread().getThreadGroup(), name);
 147     }
 148 
 149     /**
 150      * Creates a new thread group. The parent of this new group is the
 151      * specified thread group.
 152      * <p>
 153      * The {@code checkAccess} method of the parent thread group is
 154      * called with no arguments; this may result in a security exception.
 155      *
 156      * @param     parent   the parent thread group.
 157      * @param     name     the name of the new thread group, can be {@code null}


 158      * @throws    SecurityException  if the current thread cannot create a
 159      *               thread in the specified thread group.
 160      *
 161      * @deprecated
 162      * Thread groups provided a way in early JDK releases to group threads and
 163      * provide a form of <i>job control</i> for threads. Thread groups supported
 164      * the isolation of applets and defined methods intended for diagnostic
 165      * purposes. The concept of thread group is obsolete. It should be rare for
 166      * new applications to create thread groups.
 167      *
 168      * @see     java.lang.ThreadGroup#checkAccess()

 169      */
 170     @Deprecated(since = "99")
 171     public ThreadGroup(ThreadGroup parent, String name) {
 172         this(checkParentAccess(parent), parent, name);
 173     }
 174 


















 175     /**
 176      * Returns the name of this thread group.
 177      *
 178      * @return  the name of this thread group, may be {@code null}

 179      */
 180     public final String getName() {
 181         return name;
 182     }
 183 
 184     /**
 185      * Returns the parent of this thread group.
 186      * <p>
 187      * First, if the parent is not {@code null}, the
 188      * {@code checkAccess} method of the parent thread group is
 189      * called with no arguments; this may result in a security exception.
 190      *
 191      * @return  the parent of this thread group. The top-level thread group
 192      *          is the only thread group whose parent is {@code null}.
 193      * @throws  SecurityException  if the current thread cannot modify
 194      *               this thread group.
 195      * @see        java.lang.ThreadGroup#checkAccess()
 196      * @see        java.lang.SecurityException
 197      * @see        java.lang.RuntimePermission

 198      */
 199     public final ThreadGroup getParent() {
 200         if (parent != null)
 201             parent.checkAccess();
 202         return parent;
 203     }
 204 
 205     /**
 206      * Returns the maximum priority of this thread group. This is the maximum
 207      * priority for new threads created in the thread group.

 208      *
 209      * @return  the maximum priority for new threads created in the thread group

 210      * @see     #setMaxPriority

 211      */
 212     public final int getMaxPriority() {
 213         return maxPriority;
 214     }
 215 
 216     /**
 217      * Returns false.


 218      *
 219      * @return false


 220      *
 221      * @deprecated This method originally indicated if the thread group is a
 222      *             <i>daemon thread group</i> that is automatically destroyed
 223      *             when its last thread terminates. The concept of daemon
 224      *             thread group no longer exists.
 225      */
 226     @Deprecated(since="16", forRemoval=true)
 227     public final boolean isDaemon() {
 228         return false;
 229     }
 230 
 231     /**
 232      * Returns false.
 233      *
 234      * @return false

 235      *
 236      * @deprecated This method originally indicated if the thread group is
 237      *             destroyed. The ability to destroy a thread group and the
 238      *             concept of a destroyed thread group no longer exists.
 239      *
 240      * @since   1.1
 241      */
 242     @Deprecated(since="16", forRemoval=true)
 243     public boolean isDestroyed() {
 244         return false;
 245     }
 246 
 247     /**
 248      * Does nothing.






 249      *
 250      * @param daemon  ignored







 251      *
 252      * @deprecated This method originally changed the <i>daemon status</i> of
 253      *             the thread group. A daemon thread group was automatically
 254      *             destroyed when its last thread terminated. The concept of
 255      *             daemon thread group and the concept of a destroyed thread
 256      *             group no longer exists.
 257      */
 258     @Deprecated(since="16", forRemoval=true)
 259     public final void setDaemon(boolean daemon) {


 260     }
 261 
 262     /**
 263      * Sets the maximum priority of the group. Threads in the thread
 264      * group that already have a higher priority are not affected.
 265      * <p>
 266      * First, the {@code checkAccess} method of this thread group is
 267      * called with no arguments; this may result in a security exception.
 268      * <p>
 269      * If the {@code pri} argument is less than
 270      * {@link Thread#MIN_PRIORITY} or greater than
 271      * {@link Thread#MAX_PRIORITY}, the maximum priority of the group
 272      * remains unchanged.
 273      * <p>
 274      * Otherwise, the priority of this ThreadGroup object is set to the
 275      * smaller of the specified {@code pri} and the maximum permitted
 276      * priority of the parent of this thread group. (If this thread group
 277      * is the system thread group, which has no parent, then its maximum
 278      * priority is simply set to {@code pri}.) Then this method is
 279      * called recursively, with {@code pri} as its argument, for
 280      * every thread group that belongs to this thread group.
 281      *
 282      * @param      pri   the new priority of the thread group.
 283      * @throws     SecurityException  if the current thread cannot modify
 284      *               this thread group.
 285      * @see        #getMaxPriority
 286      * @see        java.lang.SecurityException
 287      * @see        java.lang.ThreadGroup#checkAccess()

 288      */
 289     public final void setMaxPriority(int pri) {
 290         checkAccess();
 291         if (pri >= Thread.MIN_PRIORITY && pri <= Thread.MAX_PRIORITY) {
 292             synchronized (this) {
 293                 if (parent == null) {
 294                     maxPriority = pri;
 295                 } else {
 296                     maxPriority = Math.min(pri, parent.maxPriority);
 297                 }
 298                 subgroups().forEach(g -> g.setMaxPriority(pri));




 299             }
 300         }



 301     }
 302 
 303     /**
 304      * Tests if this thread group is either the thread group
 305      * argument or one of its ancestor thread groups.
 306      *
 307      * @param   g   a thread group.
 308      * @return  {@code true} if this thread group is the thread group
 309      *          argument or one of its ancestor thread groups;
 310      *          {@code false} otherwise.

 311      */
 312     public final boolean parentOf(ThreadGroup g) {
 313         for (; g != null ; g = g.parent) {
 314             if (g == this) {
 315                 return true;
 316             }
 317         }
 318         return false;
 319     }
 320 
 321     /**
 322      * Determines if the currently running thread has permission to
 323      * modify this thread group.
 324      * <p>
 325      * If there is a security manager, its {@code checkAccess} method
 326      * is called with this thread group as its argument. This may result
 327      * in throwing a {@code SecurityException}.
 328      *
 329      * @throws     SecurityException  if the current thread is not allowed to
 330      *               access this thread group.
 331      * @see        java.lang.SecurityManager#checkAccess(java.lang.ThreadGroup)

 332      * @deprecated This method is only useful in conjunction with
 333      *       {@linkplain SecurityManager the Security Manager}, which is
 334      *       deprecated and subject to removal in a future release.
 335      *       Consequently, this method is also deprecated and subject to
 336      *       removal. There is no replacement for the Security Manager or this
 337      *       method.
 338      */
 339     @Deprecated(since="17", forRemoval=true)
 340     public final void checkAccess() {
 341         @SuppressWarnings("removal")
 342         SecurityManager security = System.getSecurityManager();
 343         if (security != null) {
 344             security.checkAccess(this);
 345         }
 346     }
 347 
 348     /**
 349      * Returns an estimate of the number of active (meaning
 350      * {@linkplain Thread#isAlive() alive}) threads in this thread group
 351      * and its subgroups. Recursively iterates over all subgroups in this
 352      * thread group.
 353      *
 354      * <p> The value returned is only an estimate because the number of
 355      * threads may change dynamically while this method traverses internal
 356      * data structures, and might be affected by the presence of certain
 357      * system threads. This method is intended primarily for debugging
 358      * and monitoring purposes.
 359      *
 360      * @return  an estimate of the number of active threads in this thread
 361      *          group and in any other thread group that has this thread
 362      *          group as an ancestor


 363      */
 364     public int activeCount() {
 365         int n = 0;
 366         for (Thread thread : Thread.getAllThreads()) {
 367             ThreadGroup g = thread.getThreadGroup();
 368             if (parentOf(g)) {
 369                 n++;










 370             }
 371         }
 372         return n;



 373     }
 374 
 375     /**
 376      * Copies into the specified array every active (meaning {@linkplain
 377      * Thread#isAlive() alive}) thread in this thread group and its subgroups.
 378      *
 379      * <p> An invocation of this method behaves in exactly the same
 380      * way as the invocation
 381      *
 382      * <blockquote>
 383      * {@linkplain #enumerate(Thread[], boolean) enumerate}{@code (list, true)}
 384      * </blockquote>
 385      *
 386      * @param  list
 387      *         an array into which to put the list of threads
 388      *
 389      * @return  the number of threads put into the array
 390      *
 391      * @throws  SecurityException
 392      *          if {@linkplain #checkAccess checkAccess} determines that
 393      *          the current thread cannot access this thread group


 394      */
 395     public int enumerate(Thread[] list) {
 396         return enumerate(list, true);

 397     }
 398 
 399     /**
 400      * Copies into the specified array every active (meaning {@linkplain
 401      * Thread#isAlive() alive}) thread in this thread group. If {@code
 402      * recurse} is {@code true}, this method recursively enumerates all
 403      * subgroups of this thread group and references to every active thread
 404      * in these subgroups are also included. If the array is too short to
 405      * hold all the threads, the extra threads are silently ignored.
 406      *
 407      * <p> An application might use the {@linkplain #activeCount activeCount}
 408      * method to get an estimate of how big the array should be, however
 409      * <i>if the array is too short to hold all the threads, the extra threads
 410      * are silently ignored.</i>  If it is critical to obtain every active
 411      * thread in this thread group, the caller should verify that the returned
 412      * int value is strictly less than the length of {@code list}.
 413      *
 414      * <p> Due to the inherent race condition in this method, it is recommended
 415      * that the method only be used for debugging and monitoring purposes.
 416      *
 417      * @apiNote {@linkplain java.lang.management.ThreadMXBean} supports
 418      * monitoring and management of active threads in the Java virtual
 419      * machine and may be a more suitable API some many debugging and
 420      * monitoring purposes.
 421      *
 422      * @param  list
 423      *         an array into which to put the list of threads
 424      *
 425      * @param  recurse
 426      *         if {@code true}, recursively enumerate all subgroups of this
 427      *         thread group
 428      *
 429      * @return  the number of threads put into the array
 430      *
 431      * @throws  SecurityException
 432      *          if {@linkplain #checkAccess checkAccess} determines that
 433      *          the current thread cannot access this thread group


 434      */
 435     public int enumerate(Thread[] list, boolean recurse) {
 436         Objects.requireNonNull(list);
 437         checkAccess();
 438         int n = 0;
 439         if (list.length > 0) {
 440             for (Thread thread : Thread.getAllThreads()) {
 441                 ThreadGroup g = thread.getThreadGroup();
 442                 if (g == this || (recurse && parentOf(g))) {
 443                     list[n++] = thread;
 444                     if (n == list.length) {
 445                         // list full
 446                         break;
 447                     }















 448                 }
 449             }
 450         }





 451         return n;
 452     }
 453 
 454     /**
 455      * Returns an estimate of the number of groups in this thread group and its
 456      * subgroups. Recursively iterates over all subgroups in this thread group.

 457      *
 458      * <p> The value returned is only an estimate because the number of
 459      * thread groups may change dynamically while this method traverses
 460      * internal data structures. This method is intended primarily for
 461      * debugging and monitoring purposes.
 462      *
 463      * @return  the number of thread groups with this thread group as
 464      *          an ancestor


 465      */
 466     public int activeGroupCount() {
 467         int n = 0;
 468         for (ThreadGroup group : synchronizedSubgroups()) {
 469             n = n + group.activeGroupCount() + 1;













 470         }
 471         return n;
 472     }
 473 
 474     /**
 475      * Copies into the specified array references to every subgroup in this
 476      * thread group and its subgroups.
 477      *
 478      * <p> An invocation of this method behaves in exactly the same
 479      * way as the invocation
 480      *
 481      * <blockquote>
 482      * {@linkplain #enumerate(ThreadGroup[], boolean) enumerate}{@code (list, true)}
 483      * </blockquote>
 484      *
 485      * @param  list
 486      *         an array into which to put the list of thread groups
 487      *
 488      * @return  the number of thread groups put into the array
 489      *
 490      * @throws  SecurityException
 491      *          if {@linkplain #checkAccess checkAccess} determines that
 492      *          the current thread cannot access this thread group


 493      */
 494     public int enumerate(ThreadGroup[] list) {
 495         return enumerate(list, true);

 496     }
 497 
 498     /**
 499      * Copies into the specified array references to every subgroup in this
 500      * thread group. If {@code recurse} is {@code true}, this method recursively
 501      * enumerates all subgroups of this thread group and references to every
 502      * thread group in these subgroups are also included.

 503      *
 504      * <p> An application might use the
 505      * {@linkplain #activeGroupCount activeGroupCount} method to
 506      * get an estimate of how big the array should be, however <i>if the
 507      * array is too short to hold all the thread groups, the extra thread
 508      * groups are silently ignored.</i>  If it is critical to obtain every
 509      * subgroup in this thread group, the caller should verify that
 510      * the returned int value is strictly less than the length of
 511      * {@code list}.
 512      *
 513      * <p> Due to the inherent race condition in this method, it is recommended
 514      * that the method only be used for debugging and monitoring purposes.
 515      *
 516      * @param  list
 517      *         an array into which to put the list of thread groups
 518      *
 519      * @param  recurse
 520      *         if {@code true}, recursively enumerate all subgroups
 521      *
 522      * @return  the number of thread groups put into the array
 523      *
 524      * @throws  SecurityException
 525      *          if {@linkplain #checkAccess checkAccess} determines that
 526      *          the current thread cannot access this thread group


 527      */
 528     public int enumerate(ThreadGroup[] list, boolean recurse) {
 529         Objects.requireNonNull(list);
 530         checkAccess();
 531         return enumerate(list, 0, recurse);
 532     }
 533 
 534     /**
 535      * Add a reference to each subgroup to the given array, starting at
 536      * the given index. Returns the new index.
 537      */
 538     private int enumerate(ThreadGroup[] list, int i, boolean recurse) {
 539         List<ThreadGroup> subgroups = synchronizedSubgroups();
 540         for (int j = 0; j < subgroups.size() && i < list.length; j++) {
 541             ThreadGroup group = subgroups.get(j);
 542             list[i++] = group;






 543             if (recurse) {
 544                 i = group.enumerate(list, i, true);





 545             }
 546         }
 547         return i;





 548     }
 549 
 550     /**
 551      * Throws {@code UnsupportedOperationException}.






 552      *
 553      * @deprecated This method was originally specified to stop all threads in
 554      *             the thread group. It was inherently unsafe.







 555      */
 556     @Deprecated(since="1.2", forRemoval=true)
 557     public final void stop() {
 558         throw new UnsupportedOperationException();

 559     }
 560 
 561     /**
 562      * Interrupts all active (meaning {@linkplain Thread#isAlive() alive}) in
 563      * this thread group and its subgroups.





 564      *
 565      * @throws     SecurityException  if the current thread is not allowed
 566      *               to access this thread group or any of the threads in
 567      *               the thread group.
 568      * @see        java.lang.Thread#interrupt()
 569      * @see        java.lang.SecurityException
 570      * @see        java.lang.ThreadGroup#checkAccess()
 571      * @since      1.2
 572      */
 573     public final void interrupt() {
 574         checkAccess();
 575         for (Thread thread : Thread.getAllThreads()) {
 576             ThreadGroup g = thread.getThreadGroup();
 577             if (parentOf(g)) {
 578                 thread.interrupt();







 579             }
 580         }



 581     }
 582 
 583     /**
 584      * Throws {@code UnsupportedOperationException}.






 585      *
 586      * @deprecated This method was originally specified to suspend all threads
 587      *             in the thread group.







 588      */
 589     @Deprecated(since="1.2", forRemoval=true)

 590     public final void suspend() {
 591         throw new UnsupportedOperationException();




































 592     }
 593 
 594     /**
 595      * Throws {@code UnsupportedOperationException}.






 596      *
 597      * @deprecated This method was originally specified to resume all threads
 598      *             in the thread group.









 599      */
 600     @Deprecated(since="1.2", forRemoval=true)

 601     public final void resume() {
 602         throw new UnsupportedOperationException();
















 603     }
 604 
 605     /**
 606      * Does nothing.












 607      *
 608      * @deprecated This method was originally specified to destroy an empty
 609      *             thread group. The ability to destroy a thread group no
 610      *             longer exists.
 611      */
 612     @Deprecated(since="16", forRemoval=true)
 613     public final void destroy() {












































































































































































































 614     }
 615 
 616     /**
 617      * Prints information about this thread group to the standard
 618      * output. This method is useful only for debugging.


 619      */
 620     public void list() {
 621         Map<ThreadGroup, List<Thread>> map = Stream.of(Thread.getAllThreads())
 622                 .collect(Collectors.groupingBy(Thread::getThreadGroup));
 623         list(map, System.out, 0);
 624     }
 625 
 626     private void list(Map<ThreadGroup, List<Thread>> map, PrintStream out, int indent) {
 627         out.print(" ".repeat(indent));
 628         out.println(this);
 629         indent += 4;
 630         List<Thread> threads = map.get(this);
 631         if (threads != null) {
 632             for (Thread thread : threads) {
 633                 out.print(" ".repeat(indent));
 634                 out.println(thread);










 635             }
 636         }
 637         for (ThreadGroup group : synchronizedSubgroups()) {
 638             group.list(map, out, indent);
 639         }
 640     }
 641 
 642     /**
 643      * Called by the Java Virtual Machine when a thread in this
 644      * thread group stops because of an uncaught exception, and the thread
 645      * does not have a specific {@link Thread.UncaughtExceptionHandler}
 646      * installed.
 647      * <p>
 648      * The {@code uncaughtException} method of
 649      * {@code ThreadGroup} does the following:
 650      * <ul>
 651      * <li>If this thread group has a parent thread group, the
 652      *     {@code uncaughtException} method of that parent is called
 653      *     with the same two arguments.
 654      * <li>Otherwise, this method checks to see if there is a
 655      *     {@linkplain Thread#getDefaultUncaughtExceptionHandler default
 656      *     uncaught exception handler} installed, and if so, its
 657      *     {@code uncaughtException} method is called with the same
 658      *     two arguments.
 659      * <li>Otherwise, this method determines if the {@code Throwable}
 660      *     argument is an instance of {@link ThreadDeath}. If so, nothing
 661      *     special is done. Otherwise, a message containing the
 662      *     thread's name, as returned from the thread's {@link
 663      *     Thread#getName getName} method, and a stack backtrace,
 664      *     using the {@code Throwable}'s {@link
 665      *     Throwable#printStackTrace() printStackTrace} method, is
 666      *     printed to the {@linkplain System#err standard error stream}.
 667      * </ul>
 668      * <p>
 669      * Applications can override this method in subclasses of
 670      * {@code ThreadGroup} to provide alternative handling of
 671      * uncaught exceptions.
 672      *
 673      * @param   t   the thread that is about to exit.
 674      * @param   e   the uncaught exception.

 675      */
 676     public void uncaughtException(Thread t, Throwable e) {
 677         if (parent != null) {
 678             parent.uncaughtException(t, e);
 679         } else {
 680             Thread.UncaughtExceptionHandler ueh =
 681                 Thread.getDefaultUncaughtExceptionHandler();
 682             if (ueh != null) {
 683                 ueh.uncaughtException(t, e);
 684             } else if (!(e instanceof ThreadDeath)) {
 685                 System.err.print("Exception in thread \""
 686                                  + t.getName() + "\" ");
 687                 e.printStackTrace(System.err);
 688             }
 689         }
 690     }
 691 
 692     /**
 693      * Does nothing.
 694      *
 695      * @return false
 696      *
 697      * @param b ignored
 698      *
 699      * @deprecated This method was originally intended for controlling suspension
 700      *             in low memory conditions. It was never specified.
 701      *


 702      * @since   1.1



 703      */
 704     @Deprecated(since="1.2", forRemoval=true)
 705     public boolean allowThreadSuspension(boolean b) {
 706         return false;
 707     }
 708 
 709     /**
 710      * Returns a string representation of this Thread group.
 711      *
 712      * @return  a string representation of this thread group.

 713      */
 714     public String toString() {
 715         return getClass().getName()
 716                 + "[name=" + getName()
 717                 + ",maxpri=" + getMaxPriority()
 718                 + "]";
 719     }
 720 
 721     /**
 722      * Add a strongly reachable subgroup.
 723      */
 724     private void synchronizedAddStrong(ThreadGroup group) {
 725         synchronized (this) {
 726             if (groups == null) {
 727                 groups = new ThreadGroup[4];
 728             } else if (groups.length == ngroups) {
 729                 groups = Arrays.copyOf(groups, ngroups + 4);
 730             }
 731             groups[ngroups++] = group;
 732         }
 733     }
 734 
 735     /**
 736      * Add a weakly reachable subgroup.
 737      */
 738     private void synchronizedAddWeak(ThreadGroup group) {
 739         synchronized (this) {
 740             if (weaks == null) {
 741                 @SuppressWarnings({"unchecked", "rawtypes"})
 742                 WeakReference<ThreadGroup>[] array = new WeakReference[4];
 743                 weaks = array;
 744             } else {
 745                 // expunge
 746                 for (int i = 0; i < nweaks; ) {
 747                     ThreadGroup g = weaks[i].get();
 748                     if (g == null) {
 749                         removeWeak(i);
 750                     } else {
 751                         i++;
 752                     }
 753                 }
 754                 // expand to make space if needed
 755                 if (weaks.length == nweaks) {
 756                     weaks = Arrays.copyOf(weaks, nweaks + 4);
 757                 }
 758             }
 759             weaks[nweaks++] = new WeakReference<>(group);
 760         }
 761     }
 762 
 763     /**
 764      * Remove the weakly reachable group at the given index of the weaks array.
 765      */
 766     private void removeWeak(int index) {
 767         assert Thread.holdsLock(this) && index < nweaks;
 768         int last = nweaks - 1;
 769         if (index < nweaks)
 770             weaks[index] = weaks[last];
 771         weaks[last] = null;
 772         nweaks--;
 773     }
 774 
 775     /**
 776      * Returns a snapshot of the subgroups.
 777      */
 778     private List<ThreadGroup> synchronizedSubgroups() {
 779         synchronized (this) {
 780             return subgroups();
 781         }
 782     }
 783 
 784     /**
 785      * Returns a snapshot of the subgroups.
 786      */
 787     private List<ThreadGroup> subgroups() {
 788         assert Thread.holdsLock(this);
 789         List<ThreadGroup> snapshot = new ArrayList<>();
 790         for (int i = 0; i < ngroups; i++) {
 791             snapshot.add(groups[i]);
 792         }
 793         for (int i = 0; i < nweaks; ) {
 794             ThreadGroup g = weaks[i].get();
 795             if (g == null) {
 796                 removeWeak(i);
 797             } else {
 798                 snapshot.add(g);
 799                 i++;
 800             }
 801         }
 802         return snapshot;
 803     }
 804 }
< prev index next >