Merge from the pain train
[official-gcc.git] / libjava / java / lang / ThreadGroup.java
blob0464767bf17f249b581acf353a93328d76a2a305
1 /* ThreadGroup -- a group of Threads
2 Copyright (C) 1998, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.lang;
40 import java.util.Vector;
42 /**
43 * ThreadGroup allows you to group Threads together. There is a hierarchy
44 * of ThreadGroups, and only the initial ThreadGroup has no parent. A Thread
45 * may access information about its own ThreadGroup, but not its parents or
46 * others outside the tree.
48 * @author John Keiser
49 * @author Tom Tromey
50 * @author Bryce McKinlay
51 * @author Eric Blake (ebb9@email.byu.edu)
52 * @see Thread
53 * @since 1.0
54 * @status updated to 1.4
56 public class ThreadGroup
58 /** The Initial, top-level ThreadGroup. */
59 static ThreadGroup root = new ThreadGroup();
61 /**
62 * This flag is set if an uncaught exception occurs. The runtime should
63 * check this and exit with an error status if it is set.
65 static boolean had_uncaught_exception;
67 /** The parent thread group. */
68 private final ThreadGroup parent;
70 /** The group name, non-null. */
71 final String name;
73 /** The threads in the group. */
74 private final Vector threads = new Vector();
76 /** Child thread groups, or null when this group is destroyed. */
77 private Vector groups = new Vector();
79 /** If all threads in the group are daemons. */
80 private boolean daemon_flag = false;
82 /** The maximum group priority. */
83 private int maxpri;
85 /**
86 * Hidden constructor to build the root node.
88 private ThreadGroup()
90 name = "main";
91 parent = null;
92 maxpri = Thread.MAX_PRIORITY;
95 /**
96 * Create a new ThreadGroup using the given name and the current thread's
97 * ThreadGroup as a parent. There may be a security check,
98 * <code>checkAccess</code>.
100 * @param name the name to use for the ThreadGroup
101 * @throws SecurityException if the current thread cannot create a group
102 * @see #checkAccess()
104 public ThreadGroup(String name)
106 this(Thread.currentThread().group, name);
110 * Create a new ThreadGroup using the given name and parent group. The new
111 * group inherits the maximum priority and daemon status of its parent
112 * group. There may be a security check, <code>checkAccess</code>.
114 * @param name the name to use for the ThreadGroup
115 * @param parent the ThreadGroup to use as a parent
116 * @throws NullPointerException if parent is null
117 * @throws SecurityException if the current thread cannot create a group
118 * @throws IllegalThreadStateException if the parent is destroyed
119 * @see #checkAccess()
121 public ThreadGroup(ThreadGroup parent, String name)
123 parent.checkAccess();
124 this.parent = parent;
125 this.name = name;
126 maxpri = parent.maxpri;
127 daemon_flag = parent.daemon_flag;
128 synchronized (parent)
130 if (parent.groups == null)
131 throw new IllegalThreadStateException();
132 parent.groups.add(this);
137 * Get the name of this ThreadGroup.
139 * @return the name of this ThreadGroup
141 public final String getName()
143 return name;
147 * Get the parent of this ThreadGroup. If the parent is not null, there
148 * may be a security check, <code>checkAccess</code>.
150 * @return the parent of this ThreadGroup
151 * @throws SecurityException if permission is denied
153 public final ThreadGroup getParent()
155 if (parent != null)
156 parent.checkAccess();
157 return parent;
161 * Get the maximum priority of Threads in this ThreadGroup. Threads created
162 * after this call in this group may not exceed this priority.
164 * @return the maximum priority of Threads in this ThreadGroup
166 public final int getMaxPriority()
168 return maxpri;
172 * Tell whether this ThreadGroup is a daemon group. A daemon group will
173 * be automatically destroyed when its last thread is stopped and
174 * its last thread group is destroyed.
176 * @return whether this ThreadGroup is a daemon group
178 public final boolean isDaemon()
180 return daemon_flag;
184 * Tell whether this ThreadGroup has been destroyed or not.
186 * @return whether this ThreadGroup has been destroyed or not
187 * @since 1.1
189 public synchronized boolean isDestroyed()
191 return groups == null;
195 * Set whether this ThreadGroup is a daemon group. A daemon group will be
196 * destroyed when its last thread is stopped and its last thread group is
197 * destroyed. There may be a security check, <code>checkAccess</code>.
199 * @param daemon whether this ThreadGroup should be a daemon group
200 * @throws SecurityException if you cannot modify this ThreadGroup
201 * @see #checkAccess()
203 public final void setDaemon(boolean daemon)
205 checkAccess();
206 daemon_flag = daemon;
210 * Set the maximum priority for Threads in this ThreadGroup. setMaxPriority
211 * can only be used to reduce the current maximum. If maxpri is greater
212 * than the current Maximum of the parent group, the current value is not
213 * changed. Otherwise, all groups which belong to this have their priority
214 * adjusted as well. Calling this does not affect threads already in this
215 * ThreadGroup. There may be a security check, <code>checkAccess</code>.
217 * @param maxpri the new maximum priority for this ThreadGroup
218 * @throws SecurityException if you cannot modify this ThreadGroup
219 * @see #getMaxPriority()
220 * @see #checkAccess()
222 public final synchronized void setMaxPriority(int maxpri)
224 checkAccess();
225 if (maxpri < Thread.MIN_PRIORITY || maxpri > Thread.MAX_PRIORITY)
226 return;
227 if (parent != null && maxpri > parent.maxpri)
228 maxpri = parent.maxpri;
229 this.maxpri = maxpri;
230 if (groups == null)
231 return;
232 int i = groups.size();
233 while (--i >= 0)
234 ((ThreadGroup) groups.get(i)).setMaxPriority(maxpri);
238 * Check whether this ThreadGroup is an ancestor of the specified
239 * ThreadGroup, or if they are the same.
241 * @param group the group to test on
242 * @return whether this ThreadGroup is a parent of the specified group
244 public final boolean parentOf(ThreadGroup group)
246 while (group != null)
248 if (group == this)
249 return true;
250 group = group.parent;
252 return false;
256 * Find out if the current Thread can modify this ThreadGroup. This passes
257 * the check on to <code>SecurityManager.checkAccess(this)</code>.
259 * @throws SecurityException if the current Thread cannot modify this
260 * ThreadGroup
261 * @see SecurityManager#checkAccess(ThreadGroup)
263 public final void checkAccess()
265 // Bypass System.getSecurityManager, for bootstrap efficiency.
266 SecurityManager sm = Runtime.securityManager;
267 if (sm != null)
268 sm.checkAccess(this);
272 * Return an estimate of the total number of active threads in this
273 * ThreadGroup and all its descendants. This cannot return an exact number,
274 * since the status of threads may change after they were counted; but it
275 * should be pretty close. Based on a JDC bug,
276 * <a href="http://developer.java.sun.com/developer/bugParade/bugs/4089701.html">
277 * 4089701</a>, we take active to mean isAlive().
279 * @return count of active threads in this ThreadGroup and its descendants
281 public int activeCount()
283 int total = 0;
284 if (groups == null)
285 return total;
286 int i = threads.size();
287 while (--i >= 0)
288 if (((Thread) threads.get(i)).isAlive())
289 total++;
290 i = groups.size();
291 while (--i >= 0)
292 total += ((ThreadGroup) groups.get(i)).activeCount();
293 return total;
297 * Copy all of the active Threads from this ThreadGroup and its descendants
298 * into the specified array. If the array is not big enough to hold all
299 * the Threads, extra Threads will simply not be copied. There may be a
300 * security check, <code>checkAccess</code>.
302 * @param array the array to put the threads into
303 * @return the number of threads put into the array
304 * @throws SecurityException if permission was denied
305 * @throws NullPointerException if array is null
306 * @throws ArrayStoreException if a thread does not fit in the array
307 * @see #activeCount()
308 * @see #checkAccess()
309 * @see #enumerate(Thread[], boolean)
311 public int enumerate(Thread[] array)
313 return enumerate(array, 0, true);
317 * Copy all of the active Threads from this ThreadGroup and, if desired,
318 * from its descendants, into the specified array. If the array is not big
319 * enough to hold all the Threads, extra Threads will simply not be copied.
320 * There may be a security check, <code>checkAccess</code>.
322 * @param array the array to put the threads into
323 * @param recurse whether to recurse into descendent ThreadGroups
324 * @return the number of threads put into the array
325 * @throws SecurityException if permission was denied
326 * @throws NullPointerException if array is null
327 * @throws ArrayStoreException if a thread does not fit in the array
328 * @see #activeCount()
329 * @see #checkAccess()
331 public int enumerate(Thread[] array, boolean recurse)
333 return enumerate(array, 0, recurse);
337 * Get the number of active groups in this ThreadGroup. This group itself
338 * is not included in the count. A sub-group is active if it has not been
339 * destroyed. This cannot return an exact number, since the status of
340 * threads may change after they were counted; but it should be pretty close.
342 * @return the number of active groups in this ThreadGroup
344 public int activeGroupCount()
346 if (groups == null)
347 return 0;
348 int total = groups.size();
349 int i = total;
350 while (--i >= 0)
351 total += ((ThreadGroup) groups.get(i)).activeGroupCount();
352 return total;
356 * Copy all active ThreadGroups that are descendants of this ThreadGroup
357 * into the specified array. If the array is not large enough to hold all
358 * active ThreadGroups, extra ThreadGroups simply will not be copied. There
359 * may be a security check, <code>checkAccess</code>.
361 * @param array the array to put the ThreadGroups into
362 * @return the number of ThreadGroups copied into the array
363 * @throws SecurityException if permission was denied
364 * @throws NullPointerException if array is null
365 * @throws ArrayStoreException if a group does not fit in the array
366 * @see #activeCount()
367 * @see #checkAccess()
368 * @see #enumerate(ThreadGroup[], boolean)
370 public int enumerate(ThreadGroup[] array)
372 return enumerate(array, 0, true);
376 * Copy all active ThreadGroups that are children of this ThreadGroup into
377 * the specified array, and if desired, also all descendents. If the array
378 * is not large enough to hold all active ThreadGroups, extra ThreadGroups
379 * simply will not be copied. There may be a security check,
380 * <code>checkAccess</code>.
382 * @param array the array to put the ThreadGroups into
383 * @param recurse whether to recurse into descendent ThreadGroups
384 * @return the number of ThreadGroups copied into the array
385 * @throws SecurityException if permission was denied
386 * @throws NullPointerException if array is null
387 * @throws ArrayStoreException if a group does not fit in the array
388 * @see #activeCount()
389 * @see #checkAccess()
391 public int enumerate(ThreadGroup[] array, boolean recurse)
393 return enumerate(array, 0, recurse);
397 * Stop all Threads in this ThreadGroup and its descendants.
399 * <p>This is inherently unsafe, as it can interrupt synchronized blocks and
400 * leave data in bad states. Hence, there is a security check:
401 * <code>checkAccess()</code>, followed by further checks on each thread
402 * being stopped.
404 * @throws SecurityException if permission is denied
405 * @see #checkAccess()
406 * @see Thread#stop(Throwable)
407 * @deprecated unsafe operation, try not to use
409 public final synchronized void stop()
411 checkAccess();
412 if (groups == null)
413 return;
414 int i = threads.size();
415 while (--i >= 0)
416 ((Thread) threads.get(i)).stop();
417 i = groups.size();
418 while (--i >= 0)
419 ((ThreadGroup) groups.get(i)).stop();
423 * Interrupt all Threads in this ThreadGroup and its sub-groups. There may
424 * be a security check, <code>checkAccess</code>.
426 * @throws SecurityException if permission is denied
427 * @see #checkAccess()
428 * @see Thread#interrupt()
429 * @since 1.2
431 public final synchronized void interrupt()
433 checkAccess();
434 if (groups == null)
435 return;
436 int i = threads.size();
437 while (--i >= 0)
438 ((Thread) threads.get(i)).interrupt();
439 i = groups.size();
440 while (--i >= 0)
441 ((ThreadGroup) groups.get(i)).interrupt();
445 * Suspend all Threads in this ThreadGroup and its descendants.
447 * <p>This is inherently unsafe, as suspended threads still hold locks,
448 * which can lead to deadlock. Hence, there is a security check:
449 * <code>checkAccess()</code>, followed by further checks on each thread
450 * being suspended.
452 * @throws SecurityException if permission is denied
453 * @see #checkAccess()
454 * @see Thread#suspend()
455 * @deprecated unsafe operation, try not to use
457 public final synchronized void suspend()
459 checkAccess();
460 if (groups == null)
461 return;
462 int i = threads.size();
463 while (--i >= 0)
464 ((Thread) threads.get(i)).suspend();
465 i = groups.size();
466 while (--i >= 0)
467 ((ThreadGroup) groups.get(i)).suspend();
471 * Resume all suspended Threads in this ThreadGroup and its descendants.
472 * To mirror suspend(), there is a security check:
473 * <code>checkAccess()</code>, followed by further checks on each thread
474 * being resumed.
476 * @throws SecurityException if permission is denied
477 * @see #checkAccess()
478 * @see Thread#suspend()
479 * @deprecated pointless, since suspend is deprecated
481 public final synchronized void resume()
483 checkAccess();
484 if (groups == null)
485 return;
486 int i = threads.size();
487 while (--i >= 0)
488 ((Thread) threads.get(i)).resume();
489 i = groups.size();
490 while (--i >= 0)
491 ((ThreadGroup) groups.get(i)).resume();
495 * Destroy this ThreadGroup. The group must be empty, meaning that all
496 * threads and sub-groups have completed execution. Daemon groups are
497 * destroyed automatically. There may be a security check,
498 * <code>checkAccess</code>.
500 * @throws IllegalThreadStateException if the ThreadGroup is not empty, or
501 * was previously destroyed
502 * @throws SecurityException if permission is denied
503 * @see #checkAccess()
505 public final synchronized void destroy()
507 checkAccess();
508 if (! threads.isEmpty() || groups == null)
509 throw new IllegalThreadStateException();
510 int i = groups.size();
511 while (--i >= 0)
512 ((ThreadGroup) groups.get(i)).destroy();
513 groups = null;
514 if (parent != null)
515 parent.removeGroup(this);
519 * Print out information about this ThreadGroup to System.out. This is
520 * meant for debugging purposes. <b>WARNING:</b> This method is not secure,
521 * and can print the name of threads to standard out even when you cannot
522 * otherwise get at such threads.
524 public void list()
526 list("");
530 * When a Thread in this ThreadGroup does not catch an exception, the
531 * virtual machine calls this method. The default implementation simply
532 * passes the call to the parent; then in top ThreadGroup, it will
533 * ignore ThreadDeath and print the stack trace of any other throwable.
534 * Override this method if you want to handle the exception in a different
535 * manner.
537 * @param thread the thread that exited
538 * @param t the uncaught throwable
539 * @throws NullPointerException if t is null
540 * @see ThreadDeath
541 * @see System#err
542 * @see Throwable#printStackTrace()
544 public void uncaughtException(Thread thread, Throwable t)
546 if (parent != null)
547 parent.uncaughtException(thread, t);
548 else if (! (t instanceof ThreadDeath))
550 if (t == null)
551 throw new NullPointerException();
552 had_uncaught_exception = true;
555 if (thread != null)
556 System.err.print("Exception in thread \"" + thread.name + "\" ");
557 t.printStackTrace(System.err);
559 catch (Throwable x)
561 // This means that something is badly screwed up with the runtime,
562 // or perhaps someone overloaded the Throwable.printStackTrace to
563 // die. In any case, try to deal with it gracefully.
566 System.err.println(t);
567 System.err.println("*** Got " + x
568 + " while trying to print stack trace.");
570 catch (Throwable x2)
572 // Here, someone may have overloaded t.toString() or
573 // x.toString() to die. Give up all hope; we can't even chain
574 // the exception, because the chain would likewise die.
575 System.err.println("*** Catastrophic failure while handling "
576 + "uncaught exception.");
577 throw new InternalError();
584 * Originally intended to tell the VM whether it may suspend Threads in
585 * low memory situations, this method was never implemented by Sun, and
586 * is hence a no-op.
588 * @param allow whether to allow low-memory thread suspension; ignored
589 * @return false
590 * @since 1.1
591 * @deprecated pointless, since suspend is deprecated
593 public boolean allowThreadSuspension(boolean allow)
595 return false;
599 * Return a human-readable String representing this ThreadGroup. The format
600 * of the string is:<br>
601 * <code>getClass().getName() + "[name=" + getName() + ",maxpri="
602 * + getMaxPriority() + ']'</code>.
604 * @return a human-readable String representing this ThreadGroup
606 public String toString()
608 return getClass().getName() + "[name=" + name + ",maxpri=" + maxpri + ']';
612 * Implements enumerate.
614 * @param list the array to put the threads into
615 * @param next the next open slot in the array
616 * @param recurse whether to recurse into descendent ThreadGroups
617 * @return the number of threads put into the array
618 * @throws SecurityException if permission was denied
619 * @throws NullPointerException if list is null
620 * @throws ArrayStoreException if a thread does not fit in the array
621 * @see #enumerate(Thread[])
622 * @see #enumerate(Thread[], boolean)
624 private int enumerate(Thread[] list, int next, boolean recurse)
626 checkAccess();
627 if (groups == null)
628 return next;
629 int i = threads.size();
630 while (--i >= 0 && next < list.length)
632 Thread t = (Thread) threads.get(i);
633 if (t.isAlive())
634 list[next++] = t;
636 if (recurse)
638 i = groups.size();
639 while (--i >= 0 && next < list.length)
641 ThreadGroup g = (ThreadGroup) groups.get(i);
642 next = g.enumerate(list, next, true);
645 return next;
649 * Implements enumerate.
651 * @param list the array to put the groups into
652 * @param next the next open slot in the array
653 * @param recurse whether to recurse into descendent ThreadGroups
654 * @return the number of groups put into the array
655 * @throws SecurityException if permission was denied
656 * @throws NullPointerException if list is null
657 * @throws ArrayStoreException if a group does not fit in the array
658 * @see #enumerate(ThreadGroup[])
659 * @see #enumerate(ThreadGroup[], boolean)
661 private int enumerate(ThreadGroup[] list, int next, boolean recurse)
663 checkAccess();
664 if (groups == null)
665 return next;
666 int i = groups.size();
667 while (--i >= 0 && next < list.length)
669 ThreadGroup g = (ThreadGroup) groups.get(i);
670 list[next++] = g;
671 if (recurse && next != list.length)
672 next = g.enumerate(list, next, true);
674 return next;
678 * Implements list.
680 * @param indentation the current level of indentation
681 * @see #list()
683 private void list(String indentation)
685 if (groups == null)
686 return;
687 System.out.println(indentation + this);
688 indentation += " ";
689 int i = threads.size();
690 while (--i >= 0)
691 System.out.println(indentation + threads.get(i));
692 i = groups.size();
693 while (--i >= 0)
694 ((ThreadGroup) groups.get(i)).list(indentation);
698 * Add a thread to the group. Called by Thread constructors.
700 * @param t the thread to add, non-null
701 * @throws IllegalThreadStateException if the group is destroyed
703 final synchronized void addThread(Thread t)
705 if (groups == null)
706 throw new IllegalThreadStateException("ThreadGroup is destroyed");
707 threads.add(t);
711 * Called by the VM to remove a thread that has died.
713 * @param t the thread to remove, non-null
714 * @XXX A ThreadListener to call this might be nice.
716 final synchronized void removeThread(Thread t)
718 if (groups == null)
719 return;
720 threads.remove(t);
721 t.group = null;
722 // Daemon groups are automatically destroyed when all their threads die.
723 if (daemon_flag && groups.size() == 0 && threads.size() == 0)
725 // We inline destroy to avoid the access check.
726 groups = null;
727 if (parent != null)
728 parent.removeGroup(this);
733 * Called when a group is destroyed, to remove it from its parent.
735 * @param g the destroyed group, non-null
737 final synchronized void removeGroup(ThreadGroup g)
739 groups.remove(g);
740 // Daemon groups are automatically destroyed when all their threads die.
741 if (daemon_flag && groups.size() == 0 && threads.size() == 0)
743 // We inline destroy to avoid the access check.
744 groups = null;
745 if (parent != null)
746 parent.removeGroup(this);
749 } // class ThreadGroup