Thread Group In Java Example
Chapter:
Thread
Last Updated:
13-06-2017 14:52:20 UTC
Program:
/* ............... START ............... */
public class JavaThreadGroup {
public static void main(String[] args) {
Thread t1 = Thread.currentThread();
ThreadGroup tg1 = t1.getThreadGroup();
System.out.println("Current thread's name: " + t1.getName());
System.out.println("Current thread's group name: " + tg1.getName());
Thread t2 = new Thread("new thread");
ThreadGroup tg2 = t2.getThreadGroup();
System.out.println("New thread's name: " + t2.getName());
System.out.println("New thread's group name: " + tg2.getName());
}
}
/* ............... END ............... */
Output
Current thread's name: main
Current thread's group name: main
New thread's name: new thread
New thread's group name: main
Notes:
-
The java.lang.ThreadGroup class represents a set of threads. It can also include other thread groups.
- Important methods of ThreadGroup class
- int activeCount() - returns no. of threads running in current group.
- int activeGroupCount() - returns a no. of active group in this thread group.
- void destroy() - destroys this thread group and all its sub groups.
- String getName() - returns the name of this group.
- ThreadGroup getParent() - returns the parent of this group.
- void interrupt() - interrupts all threads of this group.
- void list() - prints information of this group to standard console.
Tags
Thread Group, Java