In this tutorial, you will learn how to use ThreadGroup with example in Java.
getThreadGroup :
Every Java thread belongs to a thread group. Thread group treats all multiple threads into a single object and perform operations on all threads at once.The default thread group is main created by the JVM when first java application is started.
public final ThreadGroup getThreadGroup() : This method returns the thread group of threads to which they belongs. If thread die then it will return null.
Example : In this example we are using ThreadGroup.
public class ThreadGroupExample implements Runnable {
Thread thread;
ThreadGroup threadGroup;
ThreadGroupExample() {
threadGroup = new ThreadGroup("Thread Group Example");
thread = new Thread(threadGroup, this);
thread.start();
}
public void run() {
System.out.println(thread.getThreadGroup());
}
public static void main(String[] args) {
new ThreadGroupExample();
}
}
Output :
java.lang.ThreadGroup[name=Thread Group Example,maxpri=10]