/** * Destroys this thread group and all of its subgroups. This thread group must be empty, * indicating that all threads that had been in this thread group have since stopped. * * <p>First, the <code>checkAccess</code> method of this thread group is called with no arguments; * this may result in a security exception. * * @exception IllegalThreadStateException if the thread group is not empty or if the thread group * has already been destroyed. * @exception SecurityException if the current thread cannot modify this thread group. * @see java.lang.ThreadGroup#checkAccess() * @since JDK1.0 */ public final void destroy() { int ngroupsSnapshot; ThreadGroup[] groupsSnapshot; synchronized (this) { checkAccess(); if (destroyed || (nthreads > 0)) { throw new IllegalThreadStateException(); } ngroupsSnapshot = ngroups; if (groups != null) { groupsSnapshot = new ThreadGroup[ngroupsSnapshot]; System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot); } else { groupsSnapshot = null; } if (parent != null) { destroyed = true; ngroups = 0; groups = null; nthreads = 0; threads = null; } } for (int i = 0; i < ngroupsSnapshot; i += 1) { groupsSnapshot[i].destroy(); } if (parent != null) { parent.remove(this); } }