In this segment of tutorial we will learn about the ThreadGroup.Then We will create an example and use the Thread with the ThreadGroup.
- ThreadGroup creates the group of the threads.
- Each thread group has its name.
- Each thread uses the thread group in its object creation
Example
public class Threadgroup {
public static void main(String[] args) {
ThreadGroup grp = new ThreadGroup("group1");
mythread m1 = new mythread(grp, "thread1");
mythread m2 = new mythread(grp, "thread2");
mythread m3 = new mythread(grp, "thread3");
m1.start();
m2.start();
m3.start();
}
}
class mythread extends Thread {
public mythread(ThreadGroup g, String s) {
super(g, s);
}
@Override
public void run() {
System.out.println("Thread group, prority, thread name "
+ Thread.currentThread());
}
}
Output
Thread group, prority, thread name Thread[thread2,5,group1]
Thread group, prority, thread name Thread[thread3,5,group1]
Thread group, prority, thread name Thread[thread1,5,group1]