Home » Java programming language

Java Thread Class final ThreadGroup getThreadGroup() method with Example

Java Thread Class final ThreadGroup getThreadGroup() method: Here, we are going to learn about the final ThreadGroup getThreadGroup() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 21, 2019

Thread Class final ThreadGroup getThreadGroup()

  • This method is available in package java.lang.Thread.getThreadGroup().
  • This method is used to return the ThreadGroup of this thread [i.e. It represents that this thread basically belongs to which ThreadGroup].
  • This method is final so we can't override this method in child class.
  • The return type of this method is ThreadGroup so it returns the Threadgroup of this thread that means our thread basically belongs to which group.
  • This method does not raise any exception.


    final ThreadGroup getThreadGroup(){


We don't pass any object as a parameter in the method of the Thread.

Return value:

The return type of this method is ThreadGroup, it returns the ThreadGroup of this thread.

Java program to demonstrate example of getThreadGroup() method

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default

import java.lang.Thread;

class GetThreadGroup extends Thread {
    // Override run() of Thread class
    public void run() {
        System.out.println("We are in run() method");

class Main {
    public static void main(String[] args) {
        // Creating an object of GetThreadGroup class
        GetThreadGroup gt_group = new GetThreadGroup();

        // We are creating an object of ThreadGroup class
        ThreadGroup tg1 = new ThreadGroup("ThreadGroup 1");
        ThreadGroup tg2 = new ThreadGroup("ThreadGroup 2");

        // We are creating an object of Thread class and 
        // we are assigning the ThreadGroup of both the thread
        Thread th1 = new Thread(tg1, gt_group, "First Thread");
        Thread th2 = new Thread(tg2, gt_group, "Second Thread");

        // Calling start() method with Thread class object of Thread class

        // Here we are displaying which thread is basically 
        // belongs to which group
        System.out.println("The " + th1.getName() + " " + "is belongs to" + th1.getThreadGroup().getName());
        System.out.println("The " + th2.getName() + " " + "is belongs to" + th2.getThreadGroup().getName());


E:\Programs>javac Main.java

E:\Programs>java Main
The First Thread is belongs toThreadGroup 1
We are in run() method
We are in run() method
The Second Thread is belongs toThreadGroup 2

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.