The use of the ThreadGroup Class in Java

ThreadGroup class is used to create the group of threads. Multithreading support Thread, ThreadGroup and Runnable interface. A thread can be created by using one approach first is extending thread or second is implementing the Runnable interface.

 

 

class ourthread extends Thread

{

ourthread(String namee, ThreadGroup tt) //constructor with argument

{

super(tt,namee);

System.out.println("Thread :"+this);

start();//start thread

}

public void run()

{

try

{

for(int i=1;i<=5;i++)

{

System.out.println(getName());

Thread.sleep(1000);

}

}

catch(InterruptedException ere)

{

}

}

}

class threadgroupdemoo

{

public static void main(String ama[])

{

ThreadGroup ourgroup=new ThreadGroup("OurThreadGroup");//group of threads

ourthread aa=new ourthread("First",ourgroup);//first child thread

ourthread bb=new ourthread("Second",ourgroup);//second child thread

System.out.println("Listed output");

ourgroup.list();

}

}

Threadgroup class in Java

Explanation…..

To create child threads we build a separate class called ourthread, we invoke the constructor of thread which takes 2 parameter(threadname,threadgroup). We instantiate the threadgroup class in the main().This group comprises of 2 child threads of the ourthread. aa,bb are the childs thread which is created in main method.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @