Summary

Here are the methods of the ThreadGroup class that we introduced in this chapter:

ThreadGroup(String name)

Creates a thread group with the given name.

ThreadGroup(ThreadGroup parent, String name)

Creates a thread group that descends from the given parent and has the given name.

void suspend() (deprecated in Java 2)

Suspends all threads that descend from this thread group.

void resume() (deprecated in Java 2)

Resumes all threads that descend from this thread group.

void stop() (deprecated in Java 2)

Stops all threads that descend from this thread group.

void destroy()

Cleans up the thread group and removes it from the thread group hierarchy.

void interrupt() ( Java 2 and above only)

Interrupts all threads that descend from this thread group.

ThreadGroup getParent()

Returns the ThreadGroup reference of the parent of a thread group.

boolean parentOf(ThreadGroup g)

Returns true if the group g is an ancestor of a thread group.

int enumerate(Thread list[])

Fills in the list array with a reference to all threads in this thread group and all threads that are in groups that descend from this thread group.

int enumerate(Thread list[], boolean recurse)

Fills in the list array with a reference to all threads in this thread group and, if recurse is true, all threads that are in groups that descend from this thread group.

int activeCount()

Returns the number of active threads in this and all descending thread groups.

int enumerate(ThreadGroup list[])

Retrieves all thread group references that are descendants of the given thread group. This method operates recursively on the thread group hierarchy.

int enumerate(ThreadGroup list[], boolean recurse)

Retrieves all thread group references that are immediate descendants of the given thread group and, if recurse is true, all descendants of the current thread group.

int enumerate(ThreadGroup list[])

Retrieves all thread group references that are descendants of the given thread group. This method operates recursively on the thread group hierarchy.

int enumerate(ThreadGroup list[], boolean recurse)

Retrieves all thread group references that are immediate descendants of the given thread group and, if recurse is true, all descendants of the current thread group.

int activeGroupCount()

Returns the number of thread group descendants (at any level) of the given thread group.

void setMaxPriority(int priority)

Sets the maximum priority for the thread group.

int getMaxPriority()

Retrieves the maximum priority for the thread group.

void setDaemon(boolean on)

Changes the daemon status of the thread group.

boolean isDaemon()

Returns true if the thread group is a daemon group.

boolean isDestroyed() ( Java 1.1 and above only)

Returns a flag indicating whether the thread group has been destroyed.

String getName()

Returns the name of the thread group.

void list()

Sends a list of all the threads in the current thread group to standard out.

boolean allowThreadSuspension(boolean b) ( Java 1.1 only)

Sets the vmAllowSuspension flag of the thread group, returning the old value. When the virtual machine runs low on memory, some implementations of the virtual machine will seek to obtain memory by suspending threads in thread groups for which the vmAllowSuspension flag is set to true.

void uncaughtException(Thread t, Throwable e)

This method is called when a thread exits due to an uncaught exception; its default behavior is to print the stack trace of the thread to System.err.

In addition, we introduced these new methods of the Thread class:

Thread(ThreadGroup group, String name)

Constructs a new thread that belongs to the given thread group and has the given name.

Thread(ThreadGroup group, Runnable target)

Constructs a new thread that belongs to the given thread group and runs the given target object.

Thread(ThreadGroup group, Runnable target, String name)

Constructs a new thread that belongs to the given thread group, runs the given target object, and has the given name.

ThreadGroup getThreadGroup()

Returns the ThreadGroup reference of a thread.

Finally, we introduced these methods of the SecurityManager class that operate on threads:

void checkAccess(Thread t)

Checks if the current thread is allowed to modify the state of the thread t.

void checkAccess(ThreadGroup tg)

Checks if the current thread group is allowed to modify the state of the thread group tg.

In this chapter, we filled in the final piece of Java’s thread mechanism: a way to group threads together and operate on all threads within the group. Additionally, the ThreadGroup class forms a thread hierarchy on which security policies for Java’s thread mechanism are based.

Like the other topics in the last few chapters, the ThreadGroup class is not one that is needed by the majority of programs; it’s a special-use class for cases in which you need additional control over groups of threads. The ThreadGroup class is the last of the special-use mechanisms you need in order to complete your understanding of using threads in Java. Although we present some informative miscellaneous topics in the appendixes, the information we’ve presented in the body of this book should allow you to write productive and, if need be, very complex threaded programs in Java.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset