Home Java Thread Thread Constructors

Related Tutorials


 
 

Share on Google+Share on Google+

Thread Constructors

Advertisement
In this section, you will learn about the different types of thread constructors.

Thread Constructors

     

Several constructors are available for creating new Thread instances.
  

Thread()

Thread(String)  

Thread(Runnable)

Thread(Runnable,String)

Thread(ThreadGroup,String)

Thread(ThreadGroup,Runnable)

Thread(ThreadGroup,Runnable,String)

Thread(ThreadGroup, Runnable, String, long)

  

ThreadGroup? All threads belongs to an instance of the ThreadGroup Class. ThreadGroup is used to represent a group of threads. ThreadGroups can be shown in a hierarchical manner. There is only one root ThreadGroup that contains all other thread and groups and each subgroups can contain other groups and threads. All thread have only one thread group. And all thread groups (except the root thread group) belongs to exactly one parent thread group. Threads can access only belonging thread group.

When a new ThreadGroup is created, it is added as a member of existing ThreadGroup.
If a thread x in group1, and executes the code:

  ThreadGroup group2=new ThreadGroup(?group2?);

Then the newly formed group2 comes under group1. If you want a parent group other than default then you have to specify the parent group at the time of creation.

  ThreadGroup group2=new ThreadGroup(group2,?group3?);

Then newly formed group3 comes under the group2.

Some important methods are:

  • getName() ? This method is used to retrieve the name of particular group.
     

    ThreadGroup g=new ThreadGroup(?RoseIndia?);
      String gname=g.getName();

     

  • getParent() ? This method is used to retrieve the name of parent threadgroup of sub group.

  •    ThreadGroup group=group3.getParent();

      

  • activeGroupCount() ? This method returns the number of active thread group in a particular thread group and all its subgroups.

       int size=group.activeGroupCount();

      

  • getThreadGroup() ? This method is used to know the thread is belong to which thread group.

    ThreadGroup group=threadx.getThreadGroup();

      

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Related Tutorials

Discuss: Thread Constructors   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Harin
September 12, 2011
Thread

Sir, I had a doute abt threads, my doute is is thread constuctor is having 2 params 1 is thread or runnable and why the String param is used.......
rashmi
April 4, 2012
threading

how to start and stop a thread if there is only a single thread in the program, apart from main thread?
DMCA.com