threadgroup.java
来自「gcc3.2.1源代码」· Java 代码 · 共 628 行 · 第 1/2 页
JAVA
628 行
/* java.lang.ThreadGroup Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */ package java.lang;import java.util.Vector;import java.util.Enumeration;/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 * "The Java Language Specification", ISBN 0-201-63451-1 * plus online API docs for JDK 1.2 from http://www.javasoft.com. * Status: Complete for 1.2. Some parts from the JDK 1.0 spec only are * not implemented. */ /** * ThreadGroup allows you to group Threads together. There is a * hierarchy of ThreadGroups, and only the initial ThreadGroup has * no parent. A Thread may access information about its own * ThreadGroup, but not its parents or others outside the tree. * * @author John Keiser * @author Tom Tromey * @author Bryce McKinlay * @version 1.2.0 * @since JDK1.0 */public class ThreadGroup{ /* The Initial, top-level ThreadGroup. */ static ThreadGroup root = new ThreadGroup(); /* This flag is set if an uncaught exception occurs. The runtime should check this and exit with an error status if it is set. */ static boolean had_uncaught_exception = false; private ThreadGroup parent; private String name; private Vector threads = new Vector(); private Vector groups = new Vector(); private boolean daemon_flag = false; private int maxpri = Thread.MAX_PRIORITY; private ThreadGroup() { name = "main"; } /** Create a new ThreadGroup using the given name and the * current thread's ThreadGroup as a parent. * @param name the name to use for the ThreadGroup. */ public ThreadGroup(String name) { this (Thread.currentThread().getThreadGroup(), name); } /** Create a new ThreadGroup using the given name and * parent group. * @param name the name to use for the ThreadGroup. * @param parent the ThreadGroup to use as a parent. * @exception NullPointerException if parent is null. * @exception SecurityException if you cannot change * the intended parent group. */ public ThreadGroup(ThreadGroup parent, String name) { parent.checkAccess(); this.parent = parent; if (parent.isDestroyed()) throw new IllegalArgumentException (); this.name = name; maxpri = parent.maxpri; daemon_flag = parent.daemon_flag; parent.addGroup(this); } /** Get the name of this ThreadGroup. * @return the name of this ThreadGroup. */ public final String getName() { return name; } /** Get the parent of this ThreadGroup. * @return the parent of this ThreadGroup. */ public final ThreadGroup getParent() { return parent; } /** Set the maximum priority for Threads in this ThreadGroup. setMaxPriority * can only be used to reduce the current maximum. If maxpri * is greater than the current Maximum, the current value is not changed. * Calling this does not effect threads already in this ThreadGroup. * @param maxpri the new maximum priority for this ThreadGroup. * @exception SecurityException if you cannoy modify this ThreadGroup. */ public final synchronized void setMaxPriority(int maxpri) { checkAccess(); if (maxpri < this.maxpri && maxpri >= Thread.MIN_PRIORITY && maxpri <= Thread.MAX_PRIORITY) { this.maxpri = maxpri; } } /** Get the maximum priority of Threads in this ThreadGroup. * @return the maximum priority of Threads in this ThreadGroup. */ public final int getMaxPriority() { return maxpri; } /** Set whether this ThreadGroup is a daemon group. A daemon * group will be destroyed when its last thread is stopped and * its last thread group is destroyed. * @specnote The Java API docs indicate that the group is destroyed * when either of those happen, but that doesn't make * sense. * @param daemon whether this ThreadGroup should be a daemon group. * @exception SecurityException if you cannoy modify this ThreadGroup. */ public final void setDaemon (boolean daemon) { checkAccess(); daemon_flag = daemon; } /** Tell whether this ThreadGroup is a daemon group. A daemon * group will be destroyed when its last thread is stopped and * its last thread group is destroyed. * @specnote The Java API docs indicate that the group is destroyed * when either of those happen, but that doesn't make * sense. * @return whether this ThreadGroup is a daemon group. */ public final boolean isDaemon() { return daemon_flag; } /** Tell whether this ThreadGroup has been destroyed or not. * @return whether this ThreadGroup has been destroyed or not. */ public synchronized boolean isDestroyed() { return parent == null && this != root; } /** Check whether this ThreadGroup is an ancestor of the * specified ThreadGroup, or if they are the same. * * @param g the group to test on. * @return whether this ThreadGroup is a parent of the * specified group. */ public final boolean parentOf(ThreadGroup tg) { while (tg != null) { if (tg == this) return true; tg = tg.parent; } return false; } /** Return the total number of active threads in this ThreadGroup * and all its descendants.<P> * * This cannot return an exact number, since the status of threads * may change after they were counted. But it should be pretty * close.<P> * * @return the number of active threads in this ThreadGroup and * its descendants. * @specnote it isn't clear what the definition of an "Active" thread is. * Current JDKs regard a thread as active if has been * started and not finished. We implement this behaviour. * There is a JDC bug, <A HREF="http://developer.java.sun.com/developer/bugParade/bugs/4089701.html"> * 4089701</A>, regarding this issue. * */ public synchronized int activeCount() { int total = 0; for (int i = 0; i < threads.size(); ++i) { if (((Thread) threads.elementAt(i)).isAlive ()) ++total; } for (int i=0; i < groups.size(); i++) { ThreadGroup g = (ThreadGroup) groups.elementAt(i); total += g.activeCount(); } return total; } /** Get the number of active groups in this ThreadGroup. This group * itself is not included in the count. * @specnote it is unclear what exactly constitutes an * active ThreadGroup. Currently we assume that * all sub-groups are active, per current JDKs. * @return the number of active groups in this ThreadGroup. */ public synchronized int activeGroupCount() { int total = groups.size(); for (int i=0; i < groups.size(); i++) { ThreadGroup g = (ThreadGroup) groups.elementAt(i); total += g.activeGroupCount(); } return total; } /** Copy all of the active Threads from this ThreadGroup and * its descendants into the specified array. If the array is * not big enough to hold all the Threads, extra Threads will * simply not be copied. * * @param threads the array to put the threads into. * @return the number of threads put into the array. */ public int enumerate(Thread[] threads) { return enumerate(threads, 0, true); } /** Copy all of the active Threads from this ThreadGroup and, * if desired, from its descendants, into the specified array. * If the array is not big enough to hold all the Threads, * extra Threads will simply not be copied. * * @param threads the array to put the threads into. * @param useDescendants whether to count Threads in this * ThreadGroup's descendants or not. * @return the number of threads put into the array. */ public int enumerate(Thread[] threads, boolean useDescendants) { return enumerate(threads, 0, useDescendants); } // This actually implements enumerate. private synchronized int enumerate(Thread[] list, int next_index, boolean recurse) { Enumeration e = threads.elements(); while (e.hasMoreElements() && next_index < list.length) { Thread t = (Thread) e.nextElement(); if (t.isAlive ()) list[next_index++] = t; } if (recurse && next_index != list.length) { e = groups.elements(); while (e.hasMoreElements() && next_index < list.length) { ThreadGroup g = (ThreadGroup) e.nextElement(); next_index = g.enumerate(list, next_index, true); } } return next_index; } /** Copy all active ThreadGroups that are descendants of this * ThreadGroup into the specified array. If the array is not * large enough to hold all active ThreadGroups, extra * ThreadGroups simply will not be copied. *
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?