# Java ThreadGroup Example

A thread group represents a set of threads. A thread group not only includes threads but also other thread groups. Each child thread group in turn contains threads or more thread groups and knows the parent thread group it belongs to. Thus thread group structure is like a tree where except the top thread group all the other branches has a parent.

In this article, we will create a bunch of threads assigned to a thread group and start them. Each thread is responsible for a specific task. We will use thread group to identify the the number of active threads in this thread group. This is useful if we want to know which of the threads are still active and which is using most of the cpu time.

### ThreadGroup Example

In the below example, we create a bunch of threads, start them. Each task takes it sown time of execution. We then print the active threads found in the group.

Few things to note about:

- First we create a thread group. ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
- Create a thread array. Assign the thread to a group during its creation. We also pass a task that thread will execute. Thread[] threads = new Thread[30]; for (int i = 0; i < threads.length; i++) { String taskName = "Task-" + i; threads[i]= new Thread(threadGroup, new MyTask(taskName, i * 40), "Thread for (" + taskName + ")"); threads[i].setDaemon(true); threads[i].start(); }
- We print the active threads in the group. for (Thread thread : getThreadsInGroup(threadGroup)) { System.out.println("In group(" + thread + ")"); }
`ThreadGroup.activeCount()`

– returns an estimated number of active threads- Next, we get the active threads. Thread[] threads = new Thread[activeThreadCount]; int enumCount = group.enumerate(threads);
- If the active thread count is more than the allocated thread array size, we will create a larger size of thread array and fetch the active threads again.

*ThreadGroupExample:*

package com.javarticles.threads;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

public class ThreadGroupExample {

public static void main(String[] args) {

ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");

Thread[] threads = new Thread[30];

for (int i = 0; i < threads.length; i++) {

String taskName = "Task-" + i;

threads[i]= new Thread(threadGroup, new MyTask(taskName, i * 40),

"Thread for (" + taskName + ")");

threads[i].setDaemon(true);

threads[i].start();

}

System.out.println("Threads contained in the group");

for (Thread thread : getThreadsInGroup(threadGroup)) {

System.out.println("In group(" + thread + ")");

}

for (Thread thread : threads) {

try {

thread.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

System.out.println("All threads finished their tasks");

}

private static List getThreadsInGroup(ThreadGroup group) {

int activeThreadCount = group.activeCount();

for (int loopCount = 0; loopCount < 2; loopCount++) {

System.out.println("Try one more loop");

Thread[] threads = new Thread[activeThreadCount];

int enumCount = group.enumerate(threads);

if (enumCount < activeThreadCount) {

System.out.println("enumCount(" + enumCount + ")" +

"< threadArraySize(" + activeThreadCount + ")"); return Arrays.asList(threads).subList(0, enumCount); } System.out.println("enumCount(" + enumCount + ")" + "> threadArraySize(" + activeThreadCount + ")");

activeThreadCount += 2;

}

return Collections.emptyList();

}

}

*Output:*

Start MyTask (Task-1)

Start MyTask (Task-4)

Start MyTask (Task-0)

Task-0 done

Task-4 done

Start MyTask (Task-2)

Start MyTask (Task-3)

Start MyTask (Task-8)

Task-8 done

Task-3 done

Start MyTask (Task-12)

Task-12 done

Start MyTask (Task-13)

Start MyTask (Task-10)

Task-10 done

Start MyTask (Task-16)

Start MyTask (Task-11)

Start MyTask (Task-21)

Start MyTask (Task-9)

Task-9 done

Start MyTask (Task-5)

Task-5 done

Task-21 done

Threads contained in the group

Try one more loop

Start MyTask (Task-24)

enumCount(21)> threadArraySize(21)

Start MyTask (Task-25)

Start MyTask (Task-6)

Start MyTask (Task-26)

Task-1 done

Start MyTask (Task-28)

Start MyTask (Task-7)

Task-6 done

Start MyTask (Task-29)

Task-26 done

Task-25 done

Start MyTask (Task-23)

Task-23 done

Task-24 done

Start MyTask (Task-20)

Try one more loop

Start MyTask (Task-15)

Task-15 done

Task-11 done

Start MyTask (Task-18)

Task-16 done

Start MyTask (Task-19)

Task-19 done

Start MyTask (Task-27)

Task-27 done

Start MyTask (Task-17)

Task-13 done

Start MyTask (Task-14)

Task-2 done

Task-14 done

Task-17 done

Task-18 done

Start MyTask (Task-22)

Task-20 done

enumCount(15)< threadArraySize(23)

Task-29 done

Task-7 done

Task-28 done

Task-22 done

In group(Thread[Thread for (Task-2),5,])

In group(Thread[Thread for (Task-7),5,])

In group(Thread[Thread for (Task-11),5,])

In group(Thread[Thread for (Task-13),5,])

In group(Thread[Thread for (Task-14),5,])

In group(Thread[Thread for (Task-15),5,])

In group(Thread[Thread for (Task-16),5,])

In group(Thread[Thread for (Task-17),5,])

In group(Thread[Thread for (Task-18),5,])

In group(Thread[Thread for (Task-19),5,])

In group(Thread[Thread for (Task-20),5,])

In group(Thread[Thread for (Task-22),5,])

In group(Thread[Thread for (Task-27),5,])

In group(Thread[Thread for (Task-28),5,])

In group(Thread[Thread for (Task-29),5,])

All threads finished their tasks

### Download the source code

This was an example about thread groups. We show you how to find the still active threads belonging to thread group.

You can download the source code here: **javaThreadGroupExample.zip**