Skip to content

java.lang.ThreadGroup Class in Java

Ramesh Fadatare edited this page Aug 29, 2018 · 1 revision

Java provides a convenient way to group multiple threads in a single object. Java thread group is implemented by java.lang.ThreadGroup class.

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.

A thread is allowed to access information about its own thread group, but not to access information about its thread group's parent thread group or any other thread groups.

ThreadGroup Class Constructors

  1. ThreadGroup(String name) - Constructs a new thread group.
  2. ThreadGroup(ThreadGroup parent, String name) - Creates a new thread group.

ThreadGroup Class Methods

  • int activeCount() - Returns an estimate of the number of active threads in this thread group and its subgroups.
  • int activeGroupCount() - Returns an estimate of the number of active groups in this thread group and its subgroups.
  • void checkAccess() - Determines if the currently running thread has permission to modify this thread group.
  • void destroy() - Destroys this thread group and all of its subgroups.
  • int enumerate(Thread[] list) - Copies into the specified array every active thread in this thread group and its subgroups.
  • int enumerate(Thread[] list, boolean recurse) - Copies into the specified array every active thread in this thread group.
  • int enumerate(ThreadGroup[] list) - Copies into the specified array references to every active subgroup in this thread group and its subgroups.
  • int enumerate(ThreadGroup[] list, boolean recurse) - Copies into the specified array references to every active subgroup in this thread group.
  • int getMaxPriority() - Returns the maximum priority of this thread group.
  • String getName() - Returns the name of this thread group.
  • ThreadGroup getParent() - Returns the parent of this thread group.
  • void interrupt() - Interrupts all threads in this thread group.
  • boolean isDaemon() - Tests if this thread group is a daemon thread group.
  • boolean isDestroyed() - Tests if this thread group has been destroyed.
  • void list() - Prints information about this thread group to the standard output.
  • boolean parentOf(ThreadGroup g) - Tests if this thread group is either the thread group argument or one of its ancestor thread groups.
  • void setDaemon(boolean daemon) - Changes the daemon status of this thread group.
  • void setMaxPriority(int pri) - Sets the maximum priority of the group.
  • String toString() - Returns a string representation of this Thread group.
  • 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.

ThreadGroup Class Example

Let's create few threads and make it as a group.

public class ThreadGroupExample {
	public static void main(final String[] args) {
		final Runnable runnable1 = () -> {
			System.out.println("Runnable One");

		};

		final Runnable runnable2 = () -> {
			System.out.println("Runnable Two");

		};

		final Runnable runnable3 = () -> {
			System.out.println("Runnable Three");

		};

		final Runnable runnable4 = () -> {
			System.out.println("Runnable Four");

		};

		final ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");

		final Thread thread1 = new Thread(threadGroup, runnable1, "ThreadOne");
		thread1.start();
		final Thread thread2 = new Thread(threadGroup, runnable2, "ThreadTwo");
		thread2.start();
		final Thread thread3 = new Thread(threadGroup, runnable3, "ThreadThree");
		thread3.start();
		final Thread thread4 = new Thread(threadGroup, runnable4, "ThreadFour");
		thread4.start();

		System.out.println("Thread group name :: " + threadGroup.getName());

		threadGroup.list();
	}
}

Output:

Runnable One
Runnable Three
Thread group name :: MyThreadGroup
Runnable Two
java.lang.ThreadGroup[name=MyThreadGroup,maxpri=10]
    Runnable Four
Thread[ThreadTwo,5,MyThreadGroup]
    Thread[ThreadFour,5,MyThreadGroup]