The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Essential Java Classes
Lesson: Doing Two or More Tasks At Once: Threads

## Grouping Threads

> Every Java thread is a member of a thread group. Thread groups provide a mechanism for collecting multiple threads into a single object and manipulating those threads all at once, rather than individually. For example, you can start or suspend all the threads within a group with a single method call. Java thread groups are implemented by the `ThreadGroup` class in the `java.lang` package.

> The runtime system puts a thread into a thread group during thread construction. When you create a thread, you can either allow the runtime system to put the new thread in some reasonable default group or you can explicitly set the new thread's group. The thread is a permanent member of whatever thread group it joins upon its creation--you cannot move a thread to a new group after the thread has been created.

### The Default Thread Group

> If you create a new Thread without specifying its group in the constructor, the runtime system automatically places the new thread in the same group as the thread that created it (known as the current thread group and the current thread, respectively). So, if you leave the thread group unspecified when you create your thread, what group contains your thread?

> When a Java application first starts up, the Java runtime system creates a `ThreadGroup` named `main`. Unless specified otherwise, all new threads that you create become members of the `main` thread group.

> > Note: If you create a thread within an applet, the new thread's group may be something other than `main`, depending on the browser or viewer that the applet is running in. Refer to Threads in Applets for information about thread groups in applets.

> Many Java programmers ignore thread groups altogether and allow the runtime system to handle all of the details regarding thread groups. However, if your program creates a lot of threads that should be manipulated as a group, or if you are implementing a custom security manager, you will likely want more control over thread groups. Continue reading for more details!

### Creating a Thread Explicitly in a Group

> As mentioned previously, a thread is a permanent member of whatever thread group it joins when its created--you cannot move a thread to a new group after the thread has been created. Thus, if you wish to put your new thread in a thread group other than the default, you must specify the thread group explicitly when you create the thread. The `Thread` class has three constructors that let you set a new thread's group:

> >     public Thread(ThreadGroup group, Runnable runnable)
> >     public Thread(ThreadGroup group, String name)
> >     public Thread(ThreadGroup group, Runnable runnable, String name)

> Each of these constructors creates a new thread, initializes it based on the `Runnable` and `String` parameters, and makes the new thread a member of the specified group. For example, the following code sample creates a thread group (`myThreadGroup`) and then creates a thread (`myThread`) in that group.

> >     ThreadGroup myThreadGroup = new ThreadGroup(
> >                                         "My Group of Threads");
> >     Thread myThread = new Thread(myThreadGroup,
> >                                  "a thread for my group");

> The `ThreadGroup` passed into a `Thread` constructor does not necessarily have to be a group that you create--it can be a group created by the Java runtime system, or a group created by the application in which your applet is running.

### Getting a Thread's Group

> To find out what group a thread is in, you can call its `getThreadGroup` method:

> >     theGroup = myThread.getThreadGroup();

### The `ThreadGroup` Class

> Once you've obtained a thread's `ThreadGroup`, you can query the group for information, such as what other threads are in the group. You can also modify the threads in that group, such as suspending, resuming, or stopping them, with a single method invocation.

| Start of Tutorial > Start of Trail > Start of Lesson | Search