All ITKnowledge Source Code Search Tips Advanced Search |

| Mastering Java Threads by Marc Adler and David Herst DDC Publishing, Inc. ISBN: 1562438425 Pub Date: 05/01/99

Search this book:

Previous | Table of Contents | Next

## Chapter 8
Thread Groups

Lesson Topics

  • Introduction to ThreadGroups
  • ThreadGroup Naming
  • The ThreadGroup Class in Detail
  • Limiting Priorities
  • Java Security & checkAccess( ) Method
  • An Example of Programming with ThreadGroups

### Introduction to ThreadGroups

ThreadGroup is a class that serves to organize threads. It is analogous to the concept of directories and files within an operating system (such as UNIX or DOS). You can consider a ThreadGroup to be similar to a directory and an individual thread similar to a file.

#### ThreadGroup Characteristics

A ThreadGroup can contain a group of threads, or it can contain other ThreadGroups that, in turn, contain threads. This is the same concept of a directory that contains a combination of files and subdirectories. You can accurately envision a ThreadGroup hierarchy as a tree structure.

Global ThreadGroup Operations

Unlike the directory/file analogy, there are certain operations that you can perform on a ThreadGroup that affect every thread that is a descendant of the ThreadGroup. For example, you can—with just one call—start or stop every thread that is in the ThreadGroup.

| The main reason to use ThreadGroups is to manipulate multiple threads with a single call.

The following code illustrates how you can write a tree-like hierarchy of ThreadGroups:

    ThreadGroup g1 = new ThreadGroup (“G1”);
    ThreadGroup g2 = new ThreadGroup (g1, “G2”);
    ThreadGroup g3 = new ThreadGroup (g1, “G3”);
    ThreadGroup g4 = new ThreadGroup (g3, “G4”);
    ThreadGroup g5 = new ThreadGroup (“G5”);

Figure 8-1 The hierarchy of ThreadGroups

There are several scenarios in which you can use ThreadGroups. A common operation which developers perform on a ThreadGroup is simultaneously stopping all threads. It is easier to do this with a single call rather than cycle through all threads in your application.

For other examples, think of your stock quote monitoring application. You may want to set up a specific ThreadGroup for all threads that poll different servers for financial news. You might want to lower the priority of all news reading threads if, for example, there is heavy volume on a particular stock that you are monitoring.

### ThreadGroup Naming

ThreadGroups are organized by name. The following rules apply to ThreadGroup names:

  • each ThreadGroup must have a unique name
  • both ThreadGroup constructors take a name as an argument
  • the default ThreadGroup in a Java application is called “main”

#### The Main ThreadGroup

When your application starts, the main thread is in “main.” Unless otherwise specified, all threads will be created as part of the “main” ThreadGroup.

| If you run your Applet within a browser, the name of the root ThreadGroup might be something different. This depends on which browser you are running.

If you create a new thread in your application, and you do not specify a ThreadGroup in the thread’s constructor, the new thread is placed in the same ThreadGroup as its creator.

#### Thread Constructors

The following four thread constructor methods create the thread in the current ThreadGroup (the same ThreadGroup in which is found the new thread’s creator):

  • Thread()
  • Thread(Runnable)
  • Thread(String)
  • Thread(Runnable, String)

The following three thread constructors create the thread in a specific ThreadGroup:

  • Thread(ThreadGroup, Runnable)
  • Thread(ThreadGroup, String)
  • Thread(ThreadGroup, Runnable, String)

You can learn which ThreadGroup to which a particular thread belongs by using the getThreadGroup() method of class Thread, as shown below:

    ThreadGroup g = m_UIThread.getThreadGroup ( );

#### Enumerating Threads Within a ThreadGroup

There are methods that allow you to enumerate threads within a particular ThreadGroup, as shown below:

    ThreadGroup g = Thread.currentThread().getThreadGroup();
    int nThreads = g.getActiveCount();

    Thread threads[] = new Thread[nThreads];
    g.enumerate(threads);
    for (int i = 0; i < nThreads; i++)
    {
      doSomethingToThread(threads[i]);
    }

### The ThreadGroup Class in Detail

The java.lang package contains the implementation of the ThreadGroup class. The declaration of this class is as follows:

    public class ThreadGroup extends Object

#### Methods

The following constructor methods are available for the ThreadGroup class:

  • ThreadGroup(String): creates a new ThreadGroup
  • ThreadGroup(ThreadGroup, String): creates a new ThreadGroup with a specified name in the specified Thread group

Table 8-1 lists methods which query and change the state of Threads within a ThreadGroup.

Table 8-1: Methods which query and change the state of Threads
Method | Description
--- | ---
Destroy() | • Destroys a Thread group. • Can only be called if there are no active threads within the group. • Destroys all descendant ThreadGroups recursively. • If any of the ThreadGroups have an active thread, a IllegalThreadStateException will be thrown.
Resume() | Resumes all the Threads in this Thread group and all of its sub groups.
stop() | Stops all the Threads in this Thread group and all of its sub groups.
Suspend() | Suspends all the Threads in this Thread group and all of its sub groups.

Informational and Enumeration Methods

Table 8-2: Information & enumeration methods
Method | Description
--- | ---
ActiveCount() | Returns an estimate of the number of active Threads in the Thread group.
ActiveGroupCount() | Returns an estimate of the number of active groups in the Thread group.
enumerate(Thread[]) | Copies, into the specified array, references to every active Thread in this Thread group.
enumerate(Thread[], boolean) | Copies, into the specified array, references to every active Thread in this Thread group. The boolean is true if the ThreadGroup tree should be enumerated recursively.
enumerate(ThreadGroup[]) | Copies, into the specified array, references to every active Thread group in this Thread group.
enumerate(ThreadGroup[], boolean) | Copies, into the specified array, references to every active Thread group in this Thread group. The boolean is true if the ThreadGroup tree should be enumerated recursively.
list() | Lists this Thread group.

Previous | Table of Contents | Next

Copyright © DDC Publishing

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.