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