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