
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Thread getThreadGroup() Method
Description
The Java 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: Getting ThreadGroup of a thread
The following example shows the usage of Java Thread getThreadGroup() method. In this program, we've created a class ThreadDemo. In constructor, a new ThreadGroup is created along with a new thread using same threadgroup. Thread is started using start() method. In run() method, threadgroup is printed using getThreadGroup() method. In main method, ThreadDemo instance is created.
package com.tutorialspoint; 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(); } }
Output
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