Thread Constructors

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.