Tuesday 4 March 2014

Add thread to Thread Group

Thread class provides 4 constructors to add threads to Thread groups.

1. public Thread(ThreadGroup group, String name)
Create a new thread with the specific name and add it to the group.

2. public Thread(ThreadGroup group,Runnable target)
Creates a new thread for the Runnable target and place it in a specific group.

class ThreadGroupEx implements Runnable{
 public void run(){
  Thread t = Thread.currentThread();
  System.out.println("My Name is " + t.getName() + " I am in group" + t.getThreadGroup() );
 }

 public static void main(String args[])throws Exception{
  ThreadGroup group1 = new ThreadGroup("Group1");
  ThreadGroupEx task1 = new ThreadGroupEx();
  ThreadGroupEx task2 = new ThreadGroupEx();

  Thread t1 = new Thread(group1, task1);
  Thread t2 = new Thread(group1, task2); 

  t1.start();
  t2.start();

  t1.join();
  t2.join();
 }
}
 
Output
My Name is Thread-1 I am in groupjava.lang.ThreadGroup[name=Group1,maxpri=10]
My Name is Thread-0 I am in groupjava.lang.ThreadGroup[name=Group1,maxpri=10]
     
3. public Thread(ThreadGroup group, Runnable target, String name)
Creates a new thread for the Runnable target, assign a specific name and place it in a specific group.

class ThreadGroupEx implements Runnable{
 public void run(){
  Thread t = Thread.currentThread();
  System.out.println("My Name is " + t.getName() + " I am in group" + t.getThreadGroup() );
 }

 public static void main(String args[])throws Exception{
  ThreadGroup group1 = new ThreadGroup("Group1");

  ThreadGroupEx task1 = new ThreadGroupEx();
  ThreadGroupEx task2 = new ThreadGroupEx();

  Thread t1 = new Thread(group1, task1, "Thread1");
  Thread t2 = new Thread(group1, task2, "Thread2"); 

  t1.start();
  t2.start();

  t1.join();
  t2.join();
 }
}
    
Output
My Name is Thread2 I am in groupjava.lang.ThreadGroup[name=Group1,maxpri=10]
My Name is Thread1 I am in groupjava.lang.ThreadGroup[name=Group1,maxpri=10]
    
4. public Thread(ThreadGroup group, Runnable target, String name, long stackSize)
Creates a new thread for the Runnable target, assign a specific name and place it in a specific group. Stack size for the stack specified in bytes. The effect of stack size is totally platform dependent.

For more information on stack size see the below post.
http://selflearningjava.blogspot.in/2014/02/stack-memory.html

You are free to give any value to the stackSize variable, but it is totally the virtual machine decides. For Example If you given very minimum value for the stack, then Virtual Machine, makes it as Platform specific minimum value, If you given very maximum value, then Virtual Machine makes it as Platform specific maximum value.

Specifying a value of zero for the stackSize parameter will cause this constructor to behave exactly like the Thread(ThreadGroup, Runnable, String) constructor.

class ThreadGroupEx implements Runnable{
 public void run(){
  Thread t = Thread.currentThread();
  System.out.println("My Name is " + t.getName() + " I am in group" + t.getThreadGroup() );
 }

 public static void main(String args[])throws Exception{
  ThreadGroup group1 = new ThreadGroup("Group1");
  ThreadGroupEx task1 = new ThreadGroupEx();
  ThreadGroupEx task2 = new ThreadGroupEx();

  Thread t1 = new Thread(group1, task1, "Thread1", 1000);
  Thread t2 = new Thread(group1, task2, "Thread2", 2000); 

  t1.start();
  t2.start();

  t1.join();
  t2.join();
 }
}
   
Output

My Name is Thread1 I am in groupjava.lang.ThreadGroup[name=Group1,maxpri=10]
My Name is Thread2 I am in groupjava.lang.ThreadGroup[name=Group1,maxpri=10]




Create Thread group                                                 Get parent thread group                                                 Home

No comments:

Post a Comment