Java - Thread Group Creation

Introduction

You can create a thread group and add a new thread to that thread group.

To add a new thread in the thread group, use one the constructors of the Thread class that accepts a ThreadGroup object as an argument.

The following code places a new thread in a particular thread group:

// Create a new ThreadGroup
ThreadGroup myGroup = new ThreadGroup("My Thread Group");

// Make the new thread a member of the myGroup thread group
Thread t = new Thread(myGroup, "myThreadName");

Thread groups are arranged in a tree-like structure.

A thread group can contain another thread group.

getParent() method from ThreadGroup class returns the its parent thread group.

The parent of the top-level thread group is null.

activeCount() method from ThreadGroup class returns an estimate of the number of active threads in the group.

enumerate() method from the ThreadGroup class returns the threads in a thread group.

A thread group can work on all threads in a thread group. For example, by calling the interrupt() method of a thread group, you can interrupt all threads in the thread group.

Related Topic