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