aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/lang/ThreadGroup.java
blob: 1aa1a9a3bedd7592cd5b683e3957a160a262e34c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// ThreadGroup.java - ThreadGroup class.

/* Copyright (C) 1998, 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.lang;

import java.util.Enumeration;
import java.util.Vector;

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date August 25, 1998 
 */

/* 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.1.  Parts from the JDK 1.0 spec only are
 * not implemented.  Parts of the 1.2 spec are also not implemented.
 */

public class ThreadGroup
{
  public int activeCount ()
  {
    int ac = threads.size();
    Enumeration e = groups.elements();
    while (e.hasMoreElements())
      {
	ThreadGroup g = (ThreadGroup) e.nextElement();
	ac += g.activeCount();
      }
    return ac;
  }

  public int activeGroupCount ()
  {
    int ac = groups.size();
    Enumeration e = groups.elements();
    while (e.hasMoreElements())
      {
	ThreadGroup g = (ThreadGroup) e.nextElement();
	ac += g.activeGroupCount();
      }
    return ac;
  }

  // Deprecated in 1.2.
  public boolean allowThreadSuspension (boolean allow)
  {
    // There is no way for a Java program to determine whether this
    // has any effect whatsoever.  We don't need it.
    return true;
  }

  public final void checkAccess ()
  {
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkAccess(this);
  }

  // This is called to remove a ThreadGroup from our internal list.
  private final void remove (ThreadGroup g)
  {
    groups.removeElement(g);
    if (daemon_flag && groups.size() == 0 && threads.size() == 0)
      {
	// We inline destroy to avoid the access check.
	destroyed_flag = true;
	if (parent != null)
	  parent.remove(this);
      }
  }

  // This is called by the Thread code to remove a Thread from our
  // internal list.  FIXME: currently, it isn't called at all.  There
  // doesn't appear to be any way to remove a Thread from a
  // ThreadGroup (except the unimplemented destroy method).
  final void remove (Thread t)
  {
    threads.removeElement(t);
    if (daemon_flag && groups.size() == 0 && threads.size() == 0)
      {
	// We inline destroy to avoid the access check.
	destroyed_flag = true;
	if (parent != null)
	  parent.remove(this);
      }
  }

  // This is called by the Thread code to add a Thread to our internal
  // list.
  final void add (Thread t)
  {
    if (destroyed_flag)
      throw new IllegalThreadStateException ();

    threads.addElement(t);
  }

  // This is a helper that is used to implement the destroy method.
  private final boolean canDestroy ()
  {
    if (! threads.isEmpty())
      return false;
    Enumeration e = groups.elements();
    while (e.hasMoreElements())
      {
	ThreadGroup g = (ThreadGroup) e.nextElement();
	if (! g.canDestroy())
	  return false;
      }
    return true;
  }

  public final void destroy ()
  {
    checkAccess ();
    if (! canDestroy ())
      throw new IllegalThreadStateException ();
    destroyed_flag = true;
    if (parent != null)
      parent.remove(this);
  }

  // This actually implements enumerate.
  private final int enumerate (Thread[] ts, int next_index, boolean recurse)
  {
    Enumeration e = threads.elements();
    while (e.hasMoreElements() && next_index < ts.length)
      ts[next_index++] = (Thread) e.nextElement();
    if (recurse && next_index != ts.length)
      {
	e = groups.elements();
	while (e.hasMoreElements() && next_index < ts.length)
	  {
	    ThreadGroup g = (ThreadGroup) e.nextElement();
	    next_index = g.enumerate(ts, next_index, true);
	  }
      }
    return next_index;
  }

  public int enumerate (Thread[] ts)
  {
    return enumerate (ts, 0, true);
  }

  public int enumerate (Thread[] ts, boolean recurse)
  {
    return enumerate (ts, 0, recurse);
  }

  // This actually implements enumerate.
  private final int enumerate (ThreadGroup[] ts, int next_index,
			       boolean recurse)
  {
    Enumeration e = groups.elements();
    while (e.hasMoreElements() && next_index < ts.length)
      {
	ThreadGroup g = (ThreadGroup) e.nextElement();
	ts[next_index++] = g;
	if (recurse && next_index != ts.length)
	  next_index = g.enumerate(ts, next_index, true);
      }
    return next_index;
  }

  public int enumerate (ThreadGroup[] gs)
  {
    return enumerate (gs, 0, true);
  }

  public int enumerate (ThreadGroup[] gs, boolean recurse)
  {
    return enumerate (gs, 0, recurse);
  }

  public final int getMaxPriority ()
  {
    return maxpri;
  }

  public final String getName ()
  {
    return name;
  }

  public final ThreadGroup getParent ()
  {
    return parent;
  }

  // JDK 1.2.
  // public void interrupt ();

  public final boolean isDaemon ()
  {
    return daemon_flag;
  }

  public synchronized boolean isDestroyed ()
  {
    return destroyed_flag;
  }

  private final void list (String indentation)
  {
    System.out.print(indentation);
    System.out.println(toString ());
    String sub = indentation + "    ";
    Enumeration e = threads.elements();
    while (e.hasMoreElements())
      {
	Thread t = (Thread) e.nextElement();
	System.out.print(sub);
	System.out.println(t.toString());
      }
    e = groups.elements();
    while (e.hasMoreElements())
      {
	ThreadGroup g = (ThreadGroup) e.nextElement();
	g.list(sub);
      }
  }

  public void list ()
  {
    list ("");
  }

  public final boolean parentOf (ThreadGroup g)
  {
    while (g != null)
      {
	if (this == g)
	  return true;
	g = g.parent;
      }
    return false;
  }

  // Deprecated in 1.2.
  public final void resume ()
  {
    checkAccess ();
    Enumeration e = threads.elements();
    while (e.hasMoreElements())
      {
	Thread t = (Thread) e.nextElement();
	t.resume();
      }
    e = groups.elements();
    while (e.hasMoreElements())
      {
	ThreadGroup g = (ThreadGroup) e.nextElement();
	g.resume();
      }
  }

  public final void setDaemon (boolean daemon)
  {
    checkAccess ();
    daemon_flag = daemon;
    // FIXME: the docs don't say you are supposed to do this.  But
    // they don't say you aren't, either.
    if (groups.size() == 0 && threads.size() == 0)
      destroy ();
  }

  public final void setMaxPriority (int pri)
  {
    checkAccess ();

    // FIXME: JDK 1.2 behaviour is different: if the newMaxPriority
    // argument is < MIN_PRIORITY or > MAX_PRIORITY an
    // IllegalArgumentException should be thrown.
    if (pri >= Thread.MIN_PRIORITY && pri <= maxpri)
      {
	maxpri = pri;
	
	Enumeration e = groups.elements();
	while (e.hasMoreElements())
	  {
	    ThreadGroup g = (ThreadGroup) e.nextElement();
	    g.setMaxPriority (maxpri);
	  }
      }
  }

  // Deprecated in 1.2.
  public final void stop ()
  {
    checkAccess ();
    Enumeration e = threads.elements();
    while (e.hasMoreElements())
      {
	Thread t = (Thread) e.nextElement();
	t.stop();
      }
    e = groups.elements();
    while (e.hasMoreElements())
      {
	ThreadGroup g = (ThreadGroup) e.nextElement();
	g.stop();
      }
  }

  // Deprecated in 1.2.
  public final void suspend ()
  {
    checkAccess ();
    Enumeration e = threads.elements();
    while (e.hasMoreElements())
      {
	Thread t = (Thread) e.nextElement();
	t.suspend();
      }
    e = groups.elements();
    while (e.hasMoreElements())
      {
	ThreadGroup g = (ThreadGroup) e.nextElement();
	g.suspend();
      }
  }

  // This constructor appears in the Class Libraries book but in
  // neither the Language Spec nor the 1.2 docs.
  public ThreadGroup ()
  {
    this (Thread.currentThread().getThreadGroup(), null);
  }

  public ThreadGroup (String n)
  {
    this (Thread.currentThread().getThreadGroup(), n);
  }

  public ThreadGroup (ThreadGroup p, String n)
  {
    checkAccess ();
    if (p == null)
      throw new NullPointerException ();
    if (p.destroyed_flag)
      throw new IllegalArgumentException ();

    parent = p;
    name = n;
    maxpri = p.maxpri;
    threads = new Vector ();
    groups = new Vector ();
    daemon_flag = p.daemon_flag;
    destroyed_flag = false;
    p.groups.addElement(this);
  }

  // This is the constructor that is used when creating the very first
  // ThreadGroup.  We have an arbitrary argument here just to
  // differentiate this constructor from the others.
  private ThreadGroup (int dummy)
  {
    parent = null;
    name = "main";
    maxpri = Thread.MAX_PRIORITY;
    threads = new Vector ();
    groups = new Vector ();
    daemon_flag = false;
    destroyed_flag = false;
  }

  public String toString ()
  {
    // Language Spec and Class Libraries book disagree a bit here.  We
    // follow the Spec, but add "ThreadGroup" per the book.  We
    // include "java.lang" based on the list() example in the Class
    // Libraries book.
    return "java.lang.ThreadGroup[name=" + name + ",maxpri=" + maxpri + "]";
  }

  public void uncaughtException (Thread thread, Throwable e)
  {
    // FIXME: in 1.2, this has different semantics.  In particular if
    // this group has a parent, the exception is passed upwards and
    // not processed locally.
    if (! (e instanceof ThreadDeath))
      {
	e.printStackTrace();
      }
  }

  // Private data.
  private ThreadGroup parent;
  private String name;
  private int maxpri;
  private Vector threads;
  private Vector groups;
  private boolean daemon_flag;
  private boolean destroyed_flag;
}