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