DEV Community

Chhavi Joshi
Chhavi Joshi

Posted on • Edited on

Constructors in thread and Thread priority

There are 8 constructors in Thread class.

1) Thread():
It's the one we usually use for creating objects of Thread.

2) Thread(Runnable):
We use this constructor when we use the Runnable interface for thread creation.

3) Thread(name):
It is used to name a thread, so it's easy for debugging.

Main class:

Output:

3) Thread(Runnable, name)
When we create an object for thread using runnable interface, it is done in two step, first where we write obj using the name of class and second step where we use thread class and pass the onj name as argument of it, for example:
Class obj = new Class();
Thread ob = new Thread(obj);

but using this constructor we can directly make the object in one step.

Main class:

Priority

We can also prioritioze which thread to run first using setPriority() method

In the code above I used MAX, MIN priorities but we can also give specified priority as obj.setPriority(7);
and also return the priority of an object by printing obj.getPriority()

Output:

Top comments (0)