Java.lang.Thread.getThreadGroup() Method



Description

The java.lang.Thread.getThreadGroup() method returns the thread group to which this thread belongs. It returns null if this thread has died (been stopped).

Declaration

Following is the declaration for java.lang.Thread.getThreadGroup() method

public final ThreadGroup getThreadGroup()

Parameters

NA

Return Value

This method returns this thread's thread group.

Exception

NA

Example

The following example shows the usage of java.lang.Thread.getThreadGroup() method.

package com.tutorialspoint;

import java.lang.*;

public class ThreadDemo implements Runnable {

   Thread t;
   ThreadGroup tgrp;

   ThreadDemo() {
      tgrp = new ThreadGroup("Thread Group");
      t = new Thread(tgrp, this);
      t.start();
   }

   public void run() {
      
      // returns the thread group to which this thread belongs
      System.out.println(t.getThreadGroup());
   }

   public static void main(String[] args) {
      new ThreadDemo();
   }
}

Let us compile and run the above program, this will produce the following result −

java.lang.ThreadGroup[name=Thread Group,maxpri=10]
java_lang_thread.htm
Advertisements