Thread t1 = new Thread(); ThreadGroup tg1 = t1.getThreadGroup(); System.out.println(tg1.getName()); // prints "main"
Thread t2 = new Thread(); ThreadGroup tg2 = new ThreadGroup("testGroup"); Thread t3 = new Thread(tg2, "threadInGroup"); System.out.println(t3.getThreadGroup().getName()); // prints "testGroup"In this example, we create a new thread group named "testGroup" using the ThreadGroup constructor. We then create a new thread t3 and pass in the thread group as an argument. We then get the thread group of t3 using the getThreadGroup() method and print its name which should be "testGroup". The getThreadGroup() method is part of the java.lang.Thread class which is included in the Java Standard Library.