From 06772c7d0e9076d373ab6b1a623c617ea565b699 Mon Sep 17 00:00:00 2001 From: Bryce McKinlay Date: Wed, 28 Jun 2000 06:03:11 +0000 Subject: ThreadGroup.java: Added synchronized flag to many methods. * ThreadGroup.java: Added synchronized flag to many methods. (destroyed_flag): Removed. (isDestroyed, removeGroup, removeThread): Test for parent == null. (activeCount): Added spec note. From-SVN: r34750 --- libjava/java/lang/ThreadGroup.java | 80 +++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 36 deletions(-) (limited to 'libjava/java/lang/ThreadGroup.java') diff --git a/libjava/java/lang/ThreadGroup.java b/libjava/java/lang/ThreadGroup.java index 564c48d..f566e10 100644 --- a/libjava/java/lang/ThreadGroup.java +++ b/libjava/java/lang/ThreadGroup.java @@ -31,8 +31,8 @@ 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 beta from http://www.javasoft.com. - * Status: Complete for 1.2. Parts from the JDK 1.0 spec only are + * 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. */ @@ -44,7 +44,8 @@ import java.util.Enumeration; * * @author John Keiser * @author Tom Tromey - * @version 1.2.0, June 20, 2000 + * @author Bryce McKinlay + * @version 1.2.0 * @since JDK1.0 */ @@ -58,7 +59,6 @@ public class ThreadGroup private Vector threads = new Vector(); private Vector groups = new Vector(); private boolean daemon_flag = false; - private boolean destroyed_flag = false; private int maxpri = Thread.MAX_PRIORITY; private ThreadGroup() @@ -87,7 +87,7 @@ public class ThreadGroup { parent.checkAccess(); this.parent = parent; - if (parent.destroyed_flag) + if (parent.isDestroyed()) throw new IllegalArgumentException (); this.name = name; maxpri = parent.maxpri; @@ -118,7 +118,7 @@ public class ThreadGroup * @param maxpri the new maximum priority for this ThreadGroup. * @exception SecurityException if you cannoy modify this ThreadGroup. */ - public final void setMaxPriority(int maxpri) + public final synchronized void setMaxPriority(int maxpri) { checkAccess(); if (maxpri < this.maxpri @@ -168,9 +168,9 @@ public class ThreadGroup /** Tell whether this ThreadGroup has been destroyed or not. * @return whether this ThreadGroup has been destroyed or not. */ - public boolean isDestroyed() + public synchronized boolean isDestroyed() { - return destroyed_flag; + return parent == null && this != root; } /** Check whether this ThreadGroup is an ancestor of the @@ -200,6 +200,13 @@ public class ThreadGroup * * @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 all threads as active until they are + * finished, regardless of whether the thread has been started + * or not. We implement this behaviour. + * There is open JDC bug, + * 4089701, regarding this issue. + * */ public synchronized int activeCount() { @@ -216,17 +223,17 @@ public class ThreadGroup * 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. + * all sub-groups are active, per current JDKs. * @return the number of active groups in this ThreadGroup. */ - public int activeGroupCount() + 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; } @@ -240,7 +247,7 @@ public class ThreadGroup */ public int enumerate(Thread[] threads) { - return enumerate(threads, true); + return enumerate(threads, 0, true); } /** Copy all of the active Threads from this ThreadGroup and, @@ -259,7 +266,8 @@ public class ThreadGroup } // This actually implements enumerate. - private int enumerate (Thread[] list, int next_index, boolean recurse) + private synchronized int enumerate(Thread[] list, int next_index, + boolean recurse) { Enumeration e = threads.elements(); while (e.hasMoreElements() && next_index < list.length) @@ -286,7 +294,7 @@ public class ThreadGroup */ public int enumerate(ThreadGroup[] groups) { - return enumerate(groups, false); + return enumerate(groups, 0, true); } /** Copy all active ThreadGroups that are children of this @@ -296,18 +304,19 @@ public class ThreadGroup * ThreadGroups simply will not be copied. * * @param groups the array to put the ThreadGroups into. - * @param useDescendants whether to include all descendants + * @param recurse whether to include all descendants * of this ThreadGroup's children in determining * activeness. * @return the number of ThreadGroups copied into the array. */ - public int enumerate(ThreadGroup[] groups, boolean useDescendants) + public int enumerate(ThreadGroup[] groups, boolean recurse) { - return enumerate(groups, 0, useDescendants); + return enumerate(groups, 0, recurse); } // This actually implements enumerate. - private int enumerate (ThreadGroup[] list, int next_index, boolean recurse) + private synchronized int enumerate (ThreadGroup[] list, int next_index, + boolean recurse) { Enumeration e = groups.elements(); while (e.hasMoreElements() && next_index < list.length) @@ -326,7 +335,7 @@ public class ThreadGroup * ThreadGroups. * @since JDK1.2 */ - public final void interrupt() + public final synchronized void interrupt() { checkAccess(); for (int i=0; i < threads.size(); i++) @@ -347,7 +356,7 @@ public class ThreadGroup * ThreadGroups. * @deprecated This method calls Thread.stop(), which is dangerous. */ - public final void stop() + public final synchronized void stop() { checkAccess(); for (int i=0; i