Java : ThreadGroup with Examples

ThreadGroup (Java SE 21 & JDK 21) with Examples.
You will find code examples on most ThreadGroup methods.


Summary

A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.

Class diagram

final var group = new ThreadGroup("group-A");
System.out.println("group : " + group);

final Runnable task = () -> {
    try {
        final var current = Thread.currentThread();
        System.out.println("  run : " + current);

        TimeUnit.SECONDS.sleep(1);

    } catch (InterruptedException e) {
        System.out.println("InterruptedException!");
    }
};

final var threadA = new Thread(group, task, "name-A");
final var threadB = new Thread(group, task, "name-B");

System.out.println("-- start --");

threadA.start();
threadB.start();

System.out.println("activeCount = " + group.activeCount());

threadA.join();
threadB.join();

System.out.println("-- join --");
System.out.println("activeCount = " + group.activeCount());


// Result
// ↓
//group : java.lang.ThreadGroup[name=group-A,maxpri=10]
//-- start --
//  run : Thread[#32,name-A,5,group-A]
//  run : Thread[#33,name-B,5,group-A]
//activeCount = 2
//-- join --
//activeCount = 0

Constructors

ThreadGroup (String name)

Constructs a new thread group.

final var group1 = new ThreadGroup("abc");
System.out.println(group1.getName()); // abc

final var group2 = new ThreadGroup("XYZ");
System.out.println(group2.getName()); // XYZ

ThreadGroup (ThreadGroup parent, String name)

Creates a new thread group.

final var parent = new ThreadGroup("parent");
System.out.println(parent.getName()); // parent

final var childA = new ThreadGroup(parent, "child-A");
final var childB = new ThreadGroup(parent, "child-B");

System.out.println(childA.getName()); // child-A
System.out.println(childB.getName()); // child-B

System.out.println(parent.getParent().getName()); // main
System.out.println(childA.getParent().getName()); // parent
System.out.println(childB.getParent().getName()); // parent

Methods

int activeCount ()

Returns an estimate of the number of live platform threads in this thread group and its subgroups.

final Runnable task = () -> {
    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
        System.out.println("InterruptedException!");
    }
};

final var parent = new ThreadGroup("parent");
final var p1 = new Thread(parent, task);
final var p2 = new Thread(parent, task);

final var child = new ThreadGroup(parent, "child");
final var c1 = new Thread(child, task);
final var c2 = new Thread(child, task);

System.out.println(parent.activeCount()); // 0
System.out.println(child.activeCount()); // 0

p1.start();
p2.start();
c1.start();
c2.start();

System.out.println(parent.activeCount()); // 4
System.out.println(child.activeCount()); // 2

p1.join();
p2.join();
c1.join();
c2.join();

System.out.println(parent.activeCount()); // 0
System.out.println(child.activeCount()); // 0

int activeGroupCount ()

Returns an estimate of the number of groups in this thread group and its subgroups.

final var parent = new ThreadGroup("parent");
final var childA = new ThreadGroup(parent, "child-A");
final var childB = new ThreadGroup(parent, "child-B");

final var childChildA1 = new ThreadGroup(childA, "child-child-A1");
final var childChildA2 = new ThreadGroup(childA, "child-child-A2");

final var childChildB1 = new ThreadGroup(childB, "child-child-B1");

System.out.println(parent.activeGroupCount()); // 5
System.out.println(childA.activeGroupCount()); // 2
System.out.println(childB.activeGroupCount()); // 1

System.out.println(childChildA1.activeGroupCount()); // 0
System.out.println(childChildA2.activeGroupCount()); // 0
System.out.println(childChildB1.activeGroupCount()); // 0

final void checkAccess ()

Deprecated, for removal: This API element is subject to removal in a future version. This method is only useful in conjunction with the Security Manager, which is deprecated and subject to removal in a future release.

Deprecated.

final void destroy ()

Deprecated, for removal: This API element is subject to removal in a future version. This method was originally specified to destroy an empty thread group.

Deprecated.

int enumerate (Thread[] list)

Copies into the specified array every live platform thread in this thread group and its subgroups.

final Runnable task = () -> {
    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
        System.out.println("InterruptedException!");
    }
};

final var parent = new ThreadGroup("parent");
final var p1 = new Thread(parent, task, "p1");
final var p2 = new Thread(parent, task, "p2");

final var child = new ThreadGroup(parent, "child");
final var c1 = new Thread(child, task, "c1");
final var c2 = new Thread(child, task, "c2");

p1.start();
p2.start();
c1.start();
c2.start();

final var list = new Thread[parent.activeCount()];
final var count = parent.enumerate(list);
System.out.println("count = " + count);

System.out.println("-- list --");
for (int i = 0; i < count; i++) {
    System.out.println(list[i].getName());
}

p1.join();
p2.join();
c1.join();
c2.join();

// Result
// ↓
//count = 4
//-- list --
//p1
//p2
//c1
//c2

int enumerate (Thread[] list, boolean recurse)

Copies into the specified array every live platform thread in this thread group.

enumerate (list, true) is equivalent to enumerate(Thread[] list).

final Runnable task = () -> {
    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
        System.out.println("InterruptedException!");
    }
};

final var parent = new ThreadGroup("parent");
final var p1 = new Thread(parent, task, "p1");
final var p2 = new Thread(parent, task, "p2");

final var child = new ThreadGroup(parent, "child");
final var c1 = new Thread(child, task, "c1");
final var c2 = new Thread(child, task, "c2");

p1.start();
p2.start();
c1.start();
c2.start();

final var list = new Thread[parent.activeCount()];
final var count = parent.enumerate(list, false);
System.out.println("count = " + count);

System.out.println("-- list --");
for (int i = 0; i < count; i++) {
    System.out.println(list[i].getName());
}

p1.join();
p2.join();
c1.join();
c2.join();

// Result
// ↓
//count = 2
//-- list --
//p1
//p2

int enumerate (ThreadGroup[] list)

Copies into the specified array references to every subgroup in this thread group and its subgroups.

final var parent = new ThreadGroup("parent");
final var childA = new ThreadGroup(parent, "child-A");
final var childB = new ThreadGroup(parent, "child-B");

new ThreadGroup(childA, "child-child-A1");
new ThreadGroup(childA, "child-child-A2");

new ThreadGroup(childB, "child-child-B1");

final var list = new ThreadGroup[parent.activeGroupCount()];
final var count = parent.enumerate(list);
System.out.println("count = " + count);

System.out.println("-- list --");
for (int i = 0; i < count; i++) {
    System.out.println(list[i].getName());
}

// Result
// ↓
//count = 5
//-- list --
//child-A
//child-child-A1
//child-child-A2
//child-B
//child-child-B1

int enumerate (ThreadGroup[] list, boolean recurse)

Copies into the specified array references to every subgroup in this thread group.

enumerate (list, true) is equivalent to enumerate(ThreadGroup[] list).

final var parent = new ThreadGroup("parent");
final var childA = new ThreadGroup(parent, "child-A");
final var childB = new ThreadGroup(parent, "child-B");

new ThreadGroup(childA, "child-child-A1");
new ThreadGroup(childA, "child-child-A2");

new ThreadGroup(childB, "child-child-B1");

final var list = new ThreadGroup[parent.activeGroupCount()];
final var count = parent.enumerate(list, false);
System.out.println("count = " + count);

System.out.println("-- list --");
for (int i = 0; i < count; i++) {
    System.out.println(list[i].getName());
}

// Result
// ↓
//count = 2
//-- list --
//child-A
//child-B

final int getMaxPriority ()

Returns the maximum priority of this thread group.

final var group = new ThreadGroup("group");
System.out.println(group.getMaxPriority()); // 10

final var thread = new Thread(group, () -> {
}, "thread");
System.out.println(thread.getPriority()); // 5
final var group = new ThreadGroup("group");

group.setMaxPriority(Thread.MIN_PRIORITY);
System.out.println(group.getMaxPriority()); // 1

final var thread = new Thread(group, () -> {
}, "thread");
System.out.println(thread.getPriority()); // 1

final String getName ()

Returns the name of this thread group.

Please see ThreadGroup(String name).

final ThreadGroup getParent ()

Returns the parent of this thread group.

Please see ThreadGroup(ThreadGroup parent, String name).

final void interrupt ()

Interrupts all live platform threads in this thread group and its subgroups.

final Runnable task = () -> {
    try {
        System.out.println("task : start");
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
        System.out.println("  InterruptedException!");
    }
};

final var group = new ThreadGroup("group");
final var t1 = new Thread(group, task, "t1");
final var t2 = new Thread(group, task, "t2");

System.out.println("-- start --");

t1.start();
TimeUnit.MILLISECONDS.sleep(100);

t2.start();
TimeUnit.MILLISECONDS.sleep(100);

group.interrupt();

t1.join();
t2.join();

System.out.println("-- join --");

// Result
// ↓
//-- start --
//task : start
//task : start
//  InterruptedException!
//  InterruptedException!
//-- join --

final boolean isDaemon ()

Deprecated, for removal: This API element is subject to removal in a future version. This method originally indicated if the thread group is a daemon thread group that is automatically destroyed when its last thread terminates.

Deprecated.

boolean isDestroyed ()

Deprecated, for removal: This API element is subject to removal in a future version. This method originally indicated if the thread group is destroyed.

Deprecated.

void list ()

Prints information about this thread group to the standard output.

final var group1 = new ThreadGroup("abc");
group1.list(); // java.lang.ThreadGroup[name=abc,maxpri=10]

final var group2 = new ThreadGroup("XYZ");
group2.setMaxPriority(Thread.MIN_PRIORITY);
group2.list(); // java.lang.ThreadGroup[name=XYZ,maxpri=1]

final boolean parentOf (ThreadGroup g)

Tests if this thread group is either the thread group argument or one of its ancestor thread groups.

final var parent = new ThreadGroup("parent");
final var child = new ThreadGroup(parent, "child");
final var childChild = new ThreadGroup(child, "child-child");

System.out.println(parent.parentOf(parent)); // true
System.out.println(parent.parentOf(child)); // true
System.out.println(parent.parentOf(childChild)); // true

System.out.println(child.parentOf(parent)); // false
System.out.println(child.parentOf(child)); // true
System.out.println(child.parentOf(childChild)); // true

System.out.println(childChild.parentOf(parent)); // false
System.out.println(childChild.parentOf(child)); // false
System.out.println(childChild.parentOf(childChild)); // true

final void resume ()

Deprecated, for removal: This API element is subject to removal in a future version. This method was originally specified to resume all threads in the thread group.

Deprecated.

final void setDaemon (boolean daemon)

Deprecated, for removal: This API element is subject to removal in a future version. This method originally configured whether the thread group is a daemon thread group that is automatically destroyed when its last thread terminates.

Deprecated.

final void setMaxPriority (int pri)

Sets the maximum priority of the group.

Please see getMaxPriority().

final void stop ()

Deprecated, for removal: This API element is subject to removal in a future version. This method was originally specified to stop all threads in the thread group.

Deprecated.

final void suspend ()

Deprecated, for removal: This API element is subject to removal in a future version. This method was originally specified to suspend all threads in the thread group.

Deprecated.

String toString ()

Returns a string representation of this Thread group.

final var group1 = new ThreadGroup("abc");
final var str1 = group1.toString();
System.out.println(str1); // java.lang.ThreadGroup[name=abc,maxpri=10]

final var group2 = new ThreadGroup("XYZ");
group2.setMaxPriority(Thread.MIN_PRIORITY);

final var str2 = group2.toString();
System.out.println(str2); // java.lang.ThreadGroup[name=XYZ,maxpri=1]

void uncaughtException (Thread t, Throwable e)

Called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandler installed.

final var group = new ThreadGroup("group") {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        super.uncaughtException(t, e);

        System.out.println("uncaughtException");
        System.out.println("  t = " + t.getName());
        System.out.println("  e = " + e);
    }
};

final var threadA = new Thread(group, () -> {
    throw new IllegalStateException("Error!");
}, "thread-A");

System.out.println("-- start --");
threadA.start();

threadA.join();
System.out.println("-- join --");
System.out.println("activeCount : " + group.activeCount());

// Result
// ↓
//-- start --
//uncaughtException
//  t = thread-A
//  e = java.lang.IllegalStateException: Error!
//-- join --
//activeCount : 0

Related posts

To top of page