< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2012, 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 import jdk.internal.misc.VM;
  31 
  32 /**
  33  * A thread group represents a set of threads. In addition, a thread
  34  * group can also include other thread groups. The thread groups form
  35  * a tree in which every thread group except the initial thread group
  36  * has a parent.
  37  * <p>
  38  * A thread is allowed to access information about its own thread
  39  * group, but not to access information about its thread group's
  40  * parent thread group or any other thread groups.
  41  *
  42  * @author  unascribed
  43  * @since   1.0
  44  */
  45 /* The locking strategy for this code is to try to lock only one level of the
  46  * tree wherever possible, but otherwise to lock from the bottom up.
  47  * That is, from child thread groups to parents.
  48  * This has the advantage of limiting the number of locks that need to be held
  49  * and in particular avoids having to grab the lock for the root thread group,
  50  * (or a global lock) which would be a source of contention on a


 408      *         thread group
 409      *
 410      * @return  the number of threads put into the array
 411      *
 412      * @throws  SecurityException
 413      *          if {@linkplain #checkAccess checkAccess} determines that
 414      *          the current thread cannot access this thread group
 415      *
 416      * @since   1.0
 417      */
 418     public int enumerate(Thread list[], boolean recurse) {
 419         checkAccess();
 420         return enumerate(list, 0, recurse);
 421     }
 422 
 423     private int enumerate(Thread list[], int n, boolean recurse) {
 424         int ngroupsSnapshot = 0;
 425         ThreadGroup[] groupsSnapshot = null;
 426         synchronized (this) {
 427             if (destroyed) {
 428                 return 0;
 429             }
 430             int nt = nthreads;
 431             if (nt > list.length - n) {
 432                 nt = list.length - n;
 433             }
 434             for (int i = 0; i < nt; i++) {
 435                 if (threads[i].isAlive()) {
 436                     list[n++] = threads[i];
 437                 }
 438             }
 439             if (recurse) {
 440                 ngroupsSnapshot = ngroups;
 441                 if (groups != null) {
 442                     groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 443                 } else {
 444                     groupsSnapshot = null;
 445                 }
 446             }
 447         }
 448         if (recurse) {


 542      *         if {@code true}, recursively enumerate all subgroups
 543      *
 544      * @return  the number of thread groups put into the array
 545      *
 546      * @throws  SecurityException
 547      *          if {@linkplain #checkAccess checkAccess} determines that
 548      *          the current thread cannot access this thread group
 549      *
 550      * @since   1.0
 551      */
 552     public int enumerate(ThreadGroup list[], boolean recurse) {
 553         checkAccess();
 554         return enumerate(list, 0, recurse);
 555     }
 556 
 557     private int enumerate(ThreadGroup list[], int n, boolean recurse) {
 558         int ngroupsSnapshot = 0;
 559         ThreadGroup[] groupsSnapshot = null;
 560         synchronized (this) {
 561             if (destroyed) {
 562                 return 0;
 563             }
 564             int ng = ngroups;
 565             if (ng > list.length - n) {
 566                 ng = list.length - n;
 567             }
 568             if (ng > 0) {
 569                 System.arraycopy(groups, 0, list, n, ng);
 570                 n += ng;
 571             }
 572             if (recurse) {
 573                 ngroupsSnapshot = ngroups;
 574                 if (groups != null) {
 575                     groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 576                 } else {
 577                     groupsSnapshot = null;
 578                 }
 579             }
 580         }
 581         if (recurse) {
 582             for (int i = 0 ; i < ngroupsSnapshot ; i++) {


   1 /*
   2  * Copyright (c) 1995, 2019, 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  * @author  unascribed
  42  * @since   1.0
  43  */
  44 /* The locking strategy for this code is to try to lock only one level of the
  45  * tree wherever possible, but otherwise to lock from the bottom up.
  46  * That is, from child thread groups to parents.
  47  * This has the advantage of limiting the number of locks that need to be held
  48  * and in particular avoids having to grab the lock for the root thread group,
  49  * (or a global lock) which would be a source of contention on a


 407      *         thread group
 408      *
 409      * @return  the number of threads put into the array
 410      *
 411      * @throws  SecurityException
 412      *          if {@linkplain #checkAccess checkAccess} determines that
 413      *          the current thread cannot access this thread group
 414      *
 415      * @since   1.0
 416      */
 417     public int enumerate(Thread list[], boolean recurse) {
 418         checkAccess();
 419         return enumerate(list, 0, recurse);
 420     }
 421 
 422     private int enumerate(Thread list[], int n, boolean recurse) {
 423         int ngroupsSnapshot = 0;
 424         ThreadGroup[] groupsSnapshot = null;
 425         synchronized (this) {
 426             if (destroyed) {
 427                 return n;
 428             }
 429             int nt = nthreads;
 430             if (nt > list.length - n) {
 431                 nt = list.length - n;
 432             }
 433             for (int i = 0; i < nt; i++) {
 434                 if (threads[i].isAlive()) {
 435                     list[n++] = threads[i];
 436                 }
 437             }
 438             if (recurse) {
 439                 ngroupsSnapshot = ngroups;
 440                 if (groups != null) {
 441                     groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 442                 } else {
 443                     groupsSnapshot = null;
 444                 }
 445             }
 446         }
 447         if (recurse) {


 541      *         if {@code true}, recursively enumerate all subgroups
 542      *
 543      * @return  the number of thread groups put into the array
 544      *
 545      * @throws  SecurityException
 546      *          if {@linkplain #checkAccess checkAccess} determines that
 547      *          the current thread cannot access this thread group
 548      *
 549      * @since   1.0
 550      */
 551     public int enumerate(ThreadGroup list[], boolean recurse) {
 552         checkAccess();
 553         return enumerate(list, 0, recurse);
 554     }
 555 
 556     private int enumerate(ThreadGroup list[], int n, boolean recurse) {
 557         int ngroupsSnapshot = 0;
 558         ThreadGroup[] groupsSnapshot = null;
 559         synchronized (this) {
 560             if (destroyed) {
 561                 return n;
 562             }
 563             int ng = ngroups;
 564             if (ng > list.length - n) {
 565                 ng = list.length - n;
 566             }
 567             if (ng > 0) {
 568                 System.arraycopy(groups, 0, list, n, ng);
 569                 n += ng;
 570             }
 571             if (recurse) {
 572                 ngroupsSnapshot = ngroups;
 573                 if (groups != null) {
 574                     groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
 575                 } else {
 576                     groupsSnapshot = null;
 577                 }
 578             }
 579         }
 580         if (recurse) {
 581             for (int i = 0 ; i < ngroupsSnapshot ; i++) {


< prev index next >