ThreadGroup.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /* ThreadGroup -- a group of Threads
  2. Copyright (C) 1998, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang;
  32. import java.lang.Thread.UncaughtExceptionHandler;
  33. import java.util.Vector;
  34. /**
  35. * ThreadGroup allows you to group Threads together. There is a hierarchy
  36. * of ThreadGroups, and only the initial ThreadGroup has no parent. A Thread
  37. * may access information about its own ThreadGroup, but not its parents or
  38. * others outside the tree.
  39. *
  40. * @author John Keiser
  41. * @author Tom Tromey
  42. * @author Bryce McKinlay
  43. * @author Eric Blake (ebb9@email.byu.edu)
  44. * @see Thread
  45. * @since 1.0
  46. * @status updated to 1.4
  47. */
  48. public class ThreadGroup implements UncaughtExceptionHandler
  49. {
  50. /** The Initial, top-level ThreadGroup. */
  51. static ThreadGroup root = new ThreadGroup();
  52. /**
  53. * This flag is set if an uncaught exception occurs. The runtime should
  54. * check this and exit with an error status if it is set.
  55. */
  56. static boolean had_uncaught_exception;
  57. /** The parent thread group. */
  58. final ThreadGroup parent;
  59. /** The group name, non-null. */
  60. final String name;
  61. /** The threads in the group. */
  62. private final Vector threads = new Vector();
  63. /** Child thread groups, or null when this group is destroyed. */
  64. private Vector groups = new Vector();
  65. /** If all threads in the group are daemons. */
  66. private boolean daemon_flag = false;
  67. /** The maximum group priority. */
  68. private int maxpri;
  69. /**
  70. * Hidden constructor to build the root node.
  71. */
  72. private ThreadGroup()
  73. {
  74. name = "main";
  75. parent = null;
  76. maxpri = Thread.MAX_PRIORITY;
  77. }
  78. /**
  79. * Create a new ThreadGroup using the given name and the current thread's
  80. * ThreadGroup as a parent. There may be a security check,
  81. * <code>checkAccess</code>.
  82. *
  83. * @param name the name to use for the ThreadGroup
  84. * @throws SecurityException if the current thread cannot create a group
  85. * @see #checkAccess()
  86. */
  87. public ThreadGroup(String name)
  88. {
  89. this(Thread.currentThread().group, name);
  90. }
  91. /**
  92. * Create a new ThreadGroup using the given name and parent group. The new
  93. * group inherits the maximum priority and daemon status of its parent
  94. * group. There may be a security check, <code>checkAccess</code>.
  95. *
  96. * @param name the name to use for the ThreadGroup
  97. * @param parent the ThreadGroup to use as a parent
  98. * @throws NullPointerException if parent is null
  99. * @throws SecurityException if the current thread cannot create a group
  100. * @throws IllegalThreadStateException if the parent is destroyed
  101. * @see #checkAccess()
  102. */
  103. public ThreadGroup(ThreadGroup parent, String name)
  104. {
  105. parent.checkAccess();
  106. this.parent = parent;
  107. this.name = name;
  108. maxpri = parent.maxpri;
  109. daemon_flag = parent.daemon_flag;
  110. synchronized (parent)
  111. {
  112. if (parent.groups == null)
  113. throw new IllegalThreadStateException();
  114. parent.groups.add(this);
  115. }
  116. }
  117. /**
  118. * Get the name of this ThreadGroup.
  119. *
  120. * @return the name of this ThreadGroup
  121. */
  122. public final String getName()
  123. {
  124. return name;
  125. }
  126. /**
  127. * Get the parent of this ThreadGroup. If the parent is not null, there
  128. * may be a security check, <code>checkAccess</code>.
  129. *
  130. * @return the parent of this ThreadGroup
  131. * @throws SecurityException if permission is denied
  132. */
  133. public final ThreadGroup getParent()
  134. {
  135. if (parent != null)
  136. parent.checkAccess();
  137. return parent;
  138. }
  139. /**
  140. * Get the maximum priority of Threads in this ThreadGroup. Threads created
  141. * after this call in this group may not exceed this priority.
  142. *
  143. * @return the maximum priority of Threads in this ThreadGroup
  144. */
  145. public final int getMaxPriority()
  146. {
  147. return maxpri;
  148. }
  149. /**
  150. * Tell whether this ThreadGroup is a daemon group. A daemon group will
  151. * be automatically destroyed when its last thread is stopped and
  152. * its last thread group is destroyed.
  153. *
  154. * @return whether this ThreadGroup is a daemon group
  155. */
  156. public final boolean isDaemon()
  157. {
  158. return daemon_flag;
  159. }
  160. /**
  161. * Tell whether this ThreadGroup has been destroyed or not.
  162. *
  163. * @return whether this ThreadGroup has been destroyed or not
  164. * @since 1.1
  165. */
  166. public synchronized boolean isDestroyed()
  167. {
  168. return groups == null;
  169. }
  170. /**
  171. * Set whether this ThreadGroup is a daemon group. A daemon group will be
  172. * destroyed when its last thread is stopped and its last thread group is
  173. * destroyed. There may be a security check, <code>checkAccess</code>.
  174. *
  175. * @param daemon whether this ThreadGroup should be a daemon group
  176. * @throws SecurityException if you cannot modify this ThreadGroup
  177. * @see #checkAccess()
  178. */
  179. public final void setDaemon(boolean daemon)
  180. {
  181. checkAccess();
  182. daemon_flag = daemon;
  183. }
  184. /**
  185. * Set the maximum priority for Threads in this ThreadGroup. setMaxPriority
  186. * can only be used to reduce the current maximum. If maxpri is greater
  187. * than the current Maximum of the parent group, the current value is not
  188. * changed. Otherwise, all groups which belong to this have their priority
  189. * adjusted as well. Calling this does not affect threads already in this
  190. * ThreadGroup. There may be a security check, <code>checkAccess</code>.
  191. *
  192. * @param maxpri the new maximum priority for this ThreadGroup
  193. * @throws SecurityException if you cannot modify this ThreadGroup
  194. * @see #getMaxPriority()
  195. * @see #checkAccess()
  196. */
  197. public final synchronized void setMaxPriority(int maxpri)
  198. {
  199. checkAccess();
  200. if (maxpri < Thread.MIN_PRIORITY || maxpri > Thread.MAX_PRIORITY)
  201. return;
  202. if (parent != null && maxpri > parent.maxpri)
  203. maxpri = parent.maxpri;
  204. this.maxpri = maxpri;
  205. if (groups == null)
  206. return;
  207. int i = groups.size();
  208. while (--i >= 0)
  209. ((ThreadGroup) groups.get(i)).setMaxPriority(maxpri);
  210. }
  211. /**
  212. * Check whether this ThreadGroup is an ancestor of the specified
  213. * ThreadGroup, or if they are the same.
  214. *
  215. * @param group the group to test on
  216. * @return whether this ThreadGroup is a parent of the specified group
  217. */
  218. public final boolean parentOf(ThreadGroup group)
  219. {
  220. while (group != null)
  221. {
  222. if (group == this)
  223. return true;
  224. group = group.parent;
  225. }
  226. return false;
  227. }
  228. /**
  229. * Find out if the current Thread can modify this ThreadGroup. This passes
  230. * the check on to <code>SecurityManager.checkAccess(this)</code>.
  231. *
  232. * @throws SecurityException if the current Thread cannot modify this
  233. * ThreadGroup
  234. * @see SecurityManager#checkAccess(ThreadGroup)
  235. */
  236. public final void checkAccess()
  237. {
  238. // Bypass System.getSecurityManager, for bootstrap efficiency.
  239. SecurityManager sm = SecurityManager.current;
  240. if (sm != null)
  241. sm.checkAccess(this);
  242. }
  243. /**
  244. * Return an estimate of the total number of active threads in this
  245. * ThreadGroup and all its descendants. This cannot return an exact number,
  246. * since the status of threads may change after they were counted; but it
  247. * should be pretty close. Based on a JDC bug,
  248. * <a href="http://developer.java.sun.com/developer/bugParade/bugs/4089701.html">
  249. * 4089701</a>, we take active to mean isAlive().
  250. *
  251. * @return count of active threads in this ThreadGroup and its descendants
  252. */
  253. public int activeCount()
  254. {
  255. int total = 0;
  256. if (groups == null)
  257. return total;
  258. int i = threads.size();
  259. while (--i >= 0)
  260. if (((Thread) threads.get(i)).isAlive())
  261. total++;
  262. i = groups.size();
  263. while (--i >= 0)
  264. total += ((ThreadGroup) groups.get(i)).activeCount();
  265. return total;
  266. }
  267. /**
  268. * Copy all of the active Threads from this ThreadGroup and its descendants
  269. * into the specified array. If the array is not big enough to hold all
  270. * the Threads, extra Threads will simply not be copied. There may be a
  271. * security check, <code>checkAccess</code>.
  272. *
  273. * @param array the array to put the threads into
  274. * @return the number of threads put into the array
  275. * @throws SecurityException if permission was denied
  276. * @throws NullPointerException if array is null
  277. * @throws ArrayStoreException if a thread does not fit in the array
  278. * @see #activeCount()
  279. * @see #checkAccess()
  280. * @see #enumerate(Thread[], boolean)
  281. */
  282. public int enumerate(Thread[] array)
  283. {
  284. return enumerate(array, 0, true);
  285. }
  286. /**
  287. * Copy all of the active Threads from this ThreadGroup and, if desired,
  288. * from its descendants, into the specified array. If the array is not big
  289. * enough to hold all the Threads, extra Threads will simply not be copied.
  290. * There may be a security check, <code>checkAccess</code>.
  291. *
  292. * @param array the array to put the threads into
  293. * @param recurse whether to recurse into descendent ThreadGroups
  294. * @return the number of threads put into the array
  295. * @throws SecurityException if permission was denied
  296. * @throws NullPointerException if array is null
  297. * @throws ArrayStoreException if a thread does not fit in the array
  298. * @see #activeCount()
  299. * @see #checkAccess()
  300. */
  301. public int enumerate(Thread[] array, boolean recurse)
  302. {
  303. return enumerate(array, 0, recurse);
  304. }
  305. /**
  306. * Get the number of active groups in this ThreadGroup. This group itself
  307. * is not included in the count. A sub-group is active if it has not been
  308. * destroyed. This cannot return an exact number, since the status of
  309. * threads may change after they were counted; but it should be pretty close.
  310. *
  311. * @return the number of active groups in this ThreadGroup
  312. */
  313. public int activeGroupCount()
  314. {
  315. if (groups == null)
  316. return 0;
  317. int total = groups.size();
  318. int i = total;
  319. while (--i >= 0)
  320. total += ((ThreadGroup) groups.get(i)).activeGroupCount();
  321. return total;
  322. }
  323. /**
  324. * Copy all active ThreadGroups that are descendants of this ThreadGroup
  325. * into the specified array. If the array is not large enough to hold all
  326. * active ThreadGroups, extra ThreadGroups simply will not be copied. There
  327. * may be a security check, <code>checkAccess</code>.
  328. *
  329. * @param array the array to put the ThreadGroups into
  330. * @return the number of ThreadGroups copied into the array
  331. * @throws SecurityException if permission was denied
  332. * @throws NullPointerException if array is null
  333. * @throws ArrayStoreException if a group does not fit in the array
  334. * @see #activeCount()
  335. * @see #checkAccess()
  336. * @see #enumerate(ThreadGroup[], boolean)
  337. */
  338. public int enumerate(ThreadGroup[] array)
  339. {
  340. return enumerate(array, 0, true);
  341. }
  342. /**
  343. * Copy all active ThreadGroups that are children of this ThreadGroup into
  344. * the specified array, and if desired, also all descendents. If the array
  345. * is not large enough to hold all active ThreadGroups, extra ThreadGroups
  346. * simply will not be copied. There may be a security check,
  347. * <code>checkAccess</code>.
  348. *
  349. * @param array the array to put the ThreadGroups into
  350. * @param recurse whether to recurse into descendent ThreadGroups
  351. * @return the number of ThreadGroups copied into the array
  352. * @throws SecurityException if permission was denied
  353. * @throws NullPointerException if array is null
  354. * @throws ArrayStoreException if a group does not fit in the array
  355. * @see #activeCount()
  356. * @see #checkAccess()
  357. */
  358. public int enumerate(ThreadGroup[] array, boolean recurse)
  359. {
  360. return enumerate(array, 0, recurse);
  361. }
  362. /**
  363. * Stop all Threads in this ThreadGroup and its descendants.
  364. *
  365. * <p>This is inherently unsafe, as it can interrupt synchronized blocks and
  366. * leave data in bad states. Hence, there is a security check:
  367. * <code>checkAccess()</code>, followed by further checks on each thread
  368. * being stopped.
  369. *
  370. * @throws SecurityException if permission is denied
  371. * @see #checkAccess()
  372. * @see Thread#stop(Throwable)
  373. * @deprecated unsafe operation, try not to use
  374. */
  375. public final synchronized void stop()
  376. {
  377. checkAccess();
  378. if (groups == null)
  379. return;
  380. int i = threads.size();
  381. while (--i >= 0)
  382. ((Thread) threads.get(i)).stop();
  383. i = groups.size();
  384. while (--i >= 0)
  385. ((ThreadGroup) groups.get(i)).stop();
  386. }
  387. /**
  388. * Interrupt all Threads in this ThreadGroup and its sub-groups. There may
  389. * be a security check, <code>checkAccess</code>.
  390. *
  391. * @throws SecurityException if permission is denied
  392. * @see #checkAccess()
  393. * @see Thread#interrupt()
  394. * @since 1.2
  395. */
  396. public final synchronized void interrupt()
  397. {
  398. checkAccess();
  399. if (groups == null)
  400. return;
  401. int i = threads.size();
  402. while (--i >= 0)
  403. ((Thread) threads.get(i)).interrupt();
  404. i = groups.size();
  405. while (--i >= 0)
  406. ((ThreadGroup) groups.get(i)).interrupt();
  407. }
  408. /**
  409. * Suspend all Threads in this ThreadGroup and its descendants.
  410. *
  411. * <p>This is inherently unsafe, as suspended threads still hold locks,
  412. * which can lead to deadlock. Hence, there is a security check:
  413. * <code>checkAccess()</code>, followed by further checks on each thread
  414. * being suspended.
  415. *
  416. * @throws SecurityException if permission is denied
  417. * @see #checkAccess()
  418. * @see Thread#suspend()
  419. * @deprecated unsafe operation, try not to use
  420. */
  421. public final synchronized void suspend()
  422. {
  423. checkAccess();
  424. if (groups == null)
  425. return;
  426. int i = threads.size();
  427. while (--i >= 0)
  428. ((Thread) threads.get(i)).suspend();
  429. i = groups.size();
  430. while (--i >= 0)
  431. ((ThreadGroup) groups.get(i)).suspend();
  432. }
  433. /**
  434. * Resume all suspended Threads in this ThreadGroup and its descendants.
  435. * To mirror suspend(), there is a security check:
  436. * <code>checkAccess()</code>, followed by further checks on each thread
  437. * being resumed.
  438. *
  439. * @throws SecurityException if permission is denied
  440. * @see #checkAccess()
  441. * @see Thread#suspend()
  442. * @deprecated pointless, since suspend is deprecated
  443. */
  444. public final synchronized void resume()
  445. {
  446. checkAccess();
  447. if (groups == null)
  448. return;
  449. int i = threads.size();
  450. while (--i >= 0)
  451. ((Thread) threads.get(i)).resume();
  452. i = groups.size();
  453. while (--i >= 0)
  454. ((ThreadGroup) groups.get(i)).resume();
  455. }
  456. /**
  457. * Destroy this ThreadGroup. The group must be empty, meaning that all
  458. * threads and sub-groups have completed execution. Daemon groups are
  459. * destroyed automatically. There may be a security check,
  460. * <code>checkAccess</code>.
  461. *
  462. * @throws IllegalThreadStateException if the ThreadGroup is not empty, or
  463. * was previously destroyed
  464. * @throws SecurityException if permission is denied
  465. * @see #checkAccess()
  466. */
  467. public final synchronized void destroy()
  468. {
  469. checkAccess();
  470. if (! threads.isEmpty() || groups == null)
  471. throw new IllegalThreadStateException();
  472. int i = groups.size();
  473. while (--i >= 0)
  474. ((ThreadGroup) groups.get(i)).destroy();
  475. groups = null;
  476. if (parent != null)
  477. parent.removeGroup(this);
  478. }
  479. /**
  480. * Print out information about this ThreadGroup to System.out. This is
  481. * meant for debugging purposes. <b>WARNING:</b> This method is not secure,
  482. * and can print the name of threads to standard out even when you cannot
  483. * otherwise get at such threads.
  484. */
  485. public void list()
  486. {
  487. list("");
  488. }
  489. /**
  490. * When a Thread in this ThreadGroup does not catch an exception, the
  491. * virtual machine calls this method. The default implementation simply
  492. * passes the call to the parent; then in top ThreadGroup, it will
  493. * ignore ThreadDeath and print the stack trace of any other throwable.
  494. * Override this method if you want to handle the exception in a different
  495. * manner.
  496. *
  497. * @param thread the thread that exited
  498. * @param t the uncaught throwable
  499. * @throws NullPointerException if t is null
  500. * @see ThreadDeath
  501. * @see System#err
  502. * @see Throwable#printStackTrace()
  503. */
  504. public void uncaughtException(Thread thread, Throwable t)
  505. {
  506. if (parent != null)
  507. parent.uncaughtException(thread, t);
  508. else if (Thread.getDefaultUncaughtExceptionHandler() != null)
  509. Thread.getDefaultUncaughtExceptionHandler().uncaughtException(thread, t);
  510. else if (! (t instanceof ThreadDeath))
  511. {
  512. if (t == null)
  513. throw new NullPointerException();
  514. had_uncaught_exception = true;
  515. try
  516. {
  517. if (thread != null)
  518. System.err.print("Exception in thread \"" + thread.name + "\" ");
  519. t.printStackTrace(System.err);
  520. }
  521. catch (Throwable x)
  522. {
  523. // This means that something is badly screwed up with the runtime,
  524. // or perhaps someone overloaded the Throwable.printStackTrace to
  525. // die. In any case, try to deal with it gracefully.
  526. try
  527. {
  528. System.err.println(t);
  529. System.err.println("*** Got " + x
  530. + " while trying to print stack trace.");
  531. }
  532. catch (Throwable x2)
  533. {
  534. // Here, someone may have overloaded t.toString() or
  535. // x.toString() to die. Give up all hope; we can't even chain
  536. // the exception, because the chain would likewise die.
  537. System.err.println("*** Catastrophic failure while handling "
  538. + "uncaught exception.");
  539. throw new InternalError();
  540. }
  541. }
  542. }
  543. }
  544. /**
  545. * Originally intended to tell the VM whether it may suspend Threads in
  546. * low memory situations, this method was never implemented by Sun, and
  547. * is hence a no-op.
  548. *
  549. * @param allow whether to allow low-memory thread suspension; ignored
  550. * @return false
  551. * @since 1.1
  552. * @deprecated pointless, since suspend is deprecated
  553. */
  554. public boolean allowThreadSuspension(boolean allow)
  555. {
  556. return false;
  557. }
  558. /**
  559. * Return a human-readable String representing this ThreadGroup. The format
  560. * of the string is:<br>
  561. * <code>getClass().getName() + "[name=" + getName() + ",maxpri="
  562. * + getMaxPriority() + ']'</code>.
  563. *
  564. * @return a human-readable String representing this ThreadGroup
  565. */
  566. public String toString()
  567. {
  568. return getClass().getName() + "[name=" + name + ",maxpri=" + maxpri + ']';
  569. }
  570. /**
  571. * Implements enumerate.
  572. *
  573. * @param list the array to put the threads into
  574. * @param next the next open slot in the array
  575. * @param recurse whether to recurse into descendent ThreadGroups
  576. * @return the number of threads put into the array
  577. * @throws SecurityException if permission was denied
  578. * @throws NullPointerException if list is null
  579. * @throws ArrayStoreException if a thread does not fit in the array
  580. * @see #enumerate(Thread[])
  581. * @see #enumerate(Thread[], boolean)
  582. */
  583. private int enumerate(Thread[] list, int next, boolean recurse)
  584. {
  585. checkAccess();
  586. if (groups == null)
  587. return next;
  588. int i = threads.size();
  589. while (--i >= 0 && next < list.length)
  590. {
  591. Thread t = (Thread) threads.get(i);
  592. if (t.isAlive())
  593. list[next++] = t;
  594. }
  595. if (recurse)
  596. {
  597. i = groups.size();
  598. while (--i >= 0 && next < list.length)
  599. {
  600. ThreadGroup g = (ThreadGroup) groups.get(i);
  601. next = g.enumerate(list, next, true);
  602. }
  603. }
  604. return next;
  605. }
  606. /**
  607. * Implements enumerate.
  608. *
  609. * @param list the array to put the groups into
  610. * @param next the next open slot in the array
  611. * @param recurse whether to recurse into descendent ThreadGroups
  612. * @return the number of groups put into the array
  613. * @throws SecurityException if permission was denied
  614. * @throws NullPointerException if list is null
  615. * @throws ArrayStoreException if a group does not fit in the array
  616. * @see #enumerate(ThreadGroup[])
  617. * @see #enumerate(ThreadGroup[], boolean)
  618. */
  619. private int enumerate(ThreadGroup[] list, int next, boolean recurse)
  620. {
  621. checkAccess();
  622. if (groups == null)
  623. return next;
  624. int i = groups.size();
  625. while (--i >= 0 && next < list.length)
  626. {
  627. ThreadGroup g = (ThreadGroup) groups.get(i);
  628. list[next++] = g;
  629. if (recurse && next != list.length)
  630. next = g.enumerate(list, next, true);
  631. }
  632. return next;
  633. }
  634. /**
  635. * Implements list.
  636. *
  637. * @param indentation the current level of indentation
  638. * @see #list()
  639. */
  640. private void list(String indentation)
  641. {
  642. if (groups == null)
  643. return;
  644. System.out.println(indentation + this);
  645. indentation += " ";
  646. int i = threads.size();
  647. while (--i >= 0)
  648. System.out.println(indentation + threads.get(i));
  649. i = groups.size();
  650. while (--i >= 0)
  651. ((ThreadGroup) groups.get(i)).list(indentation);
  652. }
  653. /**
  654. * Add a thread to the group. Called by Thread constructors.
  655. *
  656. * @param t the thread to add, non-null
  657. * @throws IllegalThreadStateException if the group is destroyed
  658. */
  659. final synchronized void addThread(Thread t)
  660. {
  661. if (groups == null)
  662. throw new IllegalThreadStateException("ThreadGroup is destroyed");
  663. threads.add(t);
  664. }
  665. /**
  666. * Called by the VM to remove a thread that has died.
  667. *
  668. * @param t the thread to remove, non-null
  669. * @XXX A ThreadListener to call this might be nice.
  670. */
  671. final synchronized void removeThread(Thread t)
  672. {
  673. if (groups == null)
  674. return;
  675. threads.remove(t);
  676. t.group = null;
  677. // Daemon groups are automatically destroyed when all their threads die.
  678. if (daemon_flag && groups.size() == 0 && threads.size() == 0)
  679. {
  680. // We inline destroy to avoid the access check.
  681. groups = null;
  682. if (parent != null)
  683. parent.removeGroup(this);
  684. }
  685. }
  686. /**
  687. * Called when a group is destroyed, to remove it from its parent.
  688. *
  689. * @param g the destroyed group, non-null
  690. */
  691. final synchronized void removeGroup(ThreadGroup g)
  692. {
  693. groups.remove(g);
  694. // Daemon groups are automatically destroyed when all their threads die.
  695. if (daemon_flag && groups.size() == 0 && threads.size() == 0)
  696. {
  697. // We inline destroy to avoid the access check.
  698. groups = null;
  699. if (parent != null)
  700. parent.removeGroup(this);
  701. }
  702. }
  703. /*
  704. * Helper method for the VM. Find a Thread by its Id.
  705. *
  706. * @param id The Thread Id.
  707. * @return Thread object or null if thread doesn't exist.
  708. */
  709. static Thread getThreadFromId(long id)
  710. {
  711. return root.getThreadFromIdImpl(id);
  712. }
  713. private Thread getThreadFromIdImpl(long id)
  714. {
  715. synchronized (threads)
  716. {
  717. for (int i = 0; i < threads.size(); i++)
  718. {
  719. Thread t = (Thread) threads.get(i);
  720. if (t.getId() == id)
  721. return t;
  722. }
  723. }
  724. Vector groups = this.groups;
  725. if (groups != null)
  726. {
  727. synchronized (groups)
  728. {
  729. for (int i = 0; i < groups.size(); i++)
  730. {
  731. ThreadGroup g = (ThreadGroup) groups.get(i);
  732. Thread t = g.getThreadFromIdImpl(id);
  733. if (t != null)
  734. return t;
  735. }
  736. }
  737. }
  738. return null;
  739. }
  740. } // class ThreadGroup