Thread Group in Java

All threads belong to a thread group in Java. When you create a thread in Java it is put into a thread group specified either by you or to the same group as the thread that created it if no thread group is explicitly specified.

Default thread group

When a Java application is started one thread starts running immediately which is known as main thread in Java and this main thread belongs to a thread group called main. If you create other threads (with in the context of main thread) with out specifying thread group then these thread will also belong to main thread group.

Usage of thread group in Java

Thread groups provide a convenient way to manage multiple threads as a single thread group object. Using that thread group object you can manipulate all the threads belonging to that group as a single unit rather than doing that individually.

As example – Thread.currentThread().getThreadGroup().interrupt();

This will interrupt all the thread belonging to that thread group with a single statement.

ThreadGroup Class in Java

Java provides a class java.lang.ThreadGroup for creating thread groups in Java.

ThreadGroup Class Constructors

ThreadGroup class has two constructors for creating thread groups.

  • public ThreadGroup(String name) – Constructs a new thread group. The parent of this new group is the thread group of the currently running thread.
  • public ThreadGroup(ThreadGroup parent, String name) – Creates a new thread group. The parent of this new group is the specified thread group.

Methods in ThreadGroup Class
Some of the important methods of the ThreadGroup Class are.

  • String getName() – Returns the name of this thread group.
  • ThreadGroup getParent() – Returns the parent of this thread group.
  • boolean isDaemon() – Tests if this thread group is a daemon thread group.
  • void checkAccess() – Determines if the currently running thread has permission to modify this thread group.
  • int activeCount()– Returns an estimate of the number of active threads in this thread group and its subgroups. Recursively iterates over all subgroups in this thread group.
  • int enumerate(Thread[] list)– Copies into the specified array every active thread in this thread group and its subgroups.
  • void interrupt()– Interrupts all threads in this thread group.
  • void list()– Prints information about this thread group to the standard output.

Creating Thread in specific thread group

Thread class in Java provides constructor where you can specify a thread group. Few of those constructors are listed here.

  • public Thread(ThreadGroup group, Runnable target)
  • public Thread(ThreadGroup group, String name)
  • public Thread(ThreadGroup group, Runnable target, String name)

Thread group Java example

Output

As you can see initially when the application starts main thread starts running and it belongs to thread group main.

Then two thread groups “Group-1” and “Group-2” are created. Four thread are also created to of these threads belong to “Group-1” and other two belongs to “Group-2”.

In run() method you can see specific logic is there for threads belonging to “Group-1”.

Reference- https://docs.oracle.com/javase/10/docs/api/java/lang/ThreadGroup.html

That’s all for the topic Thread Group in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.